【问题标题】:Mock a server using Mockito使用 Mockito 模拟服务器
【发布时间】:2014-06-09 03:45:12
【问题描述】:

我正在尝试测试服务器是否会返回字符串。但是,我想在不实际连接到服务器的情况下对其进行测试。我试图通过模拟服务器来摆脱 java.net.SocketException 。我应该怎么做?

下面的代码声明了服务器的所有必要值。

@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);

下面的代码调用服务器并在 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();
    }


}

}

但是,我不断收到这个

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

【问题讨论】:

  • 您实际测试的是什么?您是否尝试从服务器模拟结果?这里有很多相互依赖的代码,所以我不是 100% 清楚你要测试是什么。
  • @Makoto 是的,我正在尝试模拟从服务器返回的结果。

标签: java mockito axis rpc xml-rpc


【解决方案1】:

我使用wiremock 效果很好。这是一个专门针对这项工作的模拟框架。

【讨论】:

    【解决方案2】:

    您也可以使用 JBoss 并运行该项目。模拟服务器时,您需要使用@Bohemian 所说的内容,因为这样更容易。但是,学习曲线很高,不建议在紧急情况下使用(顺便说一句,我是新手)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2016-12-11
      • 1970-01-01
      • 2020-04-02
      • 2019-02-07
      相关资源
      最近更新 更多