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