0

I am trying to test if the server will return the string. However, I want to test it without actually connecting to the server. I am trying to get rid of the java.net.SocketException by mocking the server. What approach should I do?

The code below declares all of the necessary values for the server.

@RunWith(MockitoJUnitRunner.class)
public class testTransfer {



@Test
public void getServerMessage() {
    //variables
    String serverhostname = "serverA";
    int portNo = 8888;
    int versionNo = 9999;
    String protocol = "tcp";
    String serverInput = "input string";

    String url = "http://localhost:8080/server/url";


    Service service =new Service();
    Call call = Mockito.mock(Call.class);
    server_string result = Mockito.mock(server_string.class);

The code below calls the server and adds values inside the xml.

    //try catch
        try {

        call=(Call)service.createCall();
        QName qn = new QName("urn:ServerApiService", "server_string" );
        call.registerTypeMapping(server_string.class, qn,
                new org.apache.axis.encoding.ser.BeanSerializerFactory(server_string.class, qn),
                new org.apache.axis.encoding.ser.BeanDeserializerFactory(server_string.class, qn));

        call.setTargetEndpointAddress( new java.net.URL(url) );
        call.addParameter("arg1", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("arg2", XMLType.XSD_INT, ParameterMode.IN);
        call.addParameter("arg3", XMLType.XSD_INT, ParameterMode.IN);
        call.addParameter("arg4", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("arg5", XMLType.XSD_STRING, ParameterMode.IN);
        call.addParameter("arg6", qn, ParameterMode.OUT);
        call.setOperationName( new QName(url, "getServerMessage") );
        call.setReturnClass(server_string.class);
        //result = (server_string) call.invoke( new Object[] {serverhostname,portNo,versionNo,protocol,serverInput} );
        result = (server_string) Mockito.when(call.invoke( new Object[] {serverhostname,portNo,versionNo,protocol,serverInput} )).thenReturn(server_string.class);
        System.out.println("Get server_string object from server ....");
                   // System.out.println("Value is "+result.value);
    } catch (ServiceException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (RemoteException e) {
        e.printStackTrace();
    }


}

}

However, I keep getting this

AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
 faultSubcode: 
 faultString: java.net.SocketException: Connection reset
 faultActor: 
 faultNode: 
 faultDetail: 
    {http://xml.apache.org/axis/}stackTrace:java.net.SocketException: Connection reset
user3720852
  • 13
  • 1
  • 5
  • What is it that you're actually testing? Are you trying to mock a result back from the server? There's a lot of interdependent code here, so I'm not 100% clear on what it is that you're trying to *test*. – Makoto Jun 09 '14 at 03:49
  • @Makoto Yes, I am trying to mock a result back from the server. – user3720852 Jun 09 '14 at 03:55

2 Answers2

3

I have used wiremock to good effect. It's a mocking framework specifically for this job.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You may also use JBoss and run the project. When mocking the server you need to use what @Bohemian said because it is much easier that way. However, the learning curve is high and is not recommended for emergency situations(i'm a newbie btw).

user3720852
  • 13
  • 1
  • 5