莫皮托存根

  • 本文关键字:存根 junit mockito
  • 更新时间 :
  • 英文 :


>下面列出的是我尝试使用Junit和Mockito测试的方法

爪哇代码

public String getAuthenticationService() {
        Authentication endpoint;
        String token = "";
        try {
            URL wsdlURL = new URL(authenticationURL);
            SoapService service = new SoapService(wsdlURL,
                    new QName("SomeQName",
                            "SoapService"));
            endpoint = service.getAuthenticationPort();
            token = endpoint.authenticate(username, password);
        } catch (Exception e) {
            throw new GenericException(
                    "OpenText AuthenticationService not working Error is "
                            + e.toString());
        }
        return token;
    }

朱尼特方法

public void testGetAuthenticationService()
            throws AuthenticationException_Exception {
        AuthenticationService mockService = Mockito
                .mock(AuthenticationService.class);
        Authentication mockEndpoint = Mockito.mock(Authentication.class);
        Mockito.when(mockService.getAuthenticationPort()).thenReturn(
                mockEndpoint);
        Mockito.when(mockEndpoint.authenticate(username, password)).thenReturn(
                token);
}

当我运行 Junit 测试用例时,endpoint.authenticate 尝试连接到 actaul soap 服务,并且方法存根不起作用,我在这里做错了什么

你的mockService似乎是你SoapService的一个很好的替代品,但你没有给自己一个机会在你的代码中引用它。你的测试调用代码,代码调用 SoapService 构造函数,所以你得到一个真正的服务。请考虑以下重构:

public String getAuthenticationService() {
    try {
        URL wsdlURL = new URL(authenticationURL);
        SoapService service = new SoapService(wsdlURL,
            new QName("SomeQName", "SoapService"));
        return getAuthenticationService(service);
    } catch (Exception e) {
        throw new GenericException(
                "OpenText AuthenticationService not working Error is "
                        + e.toString());
    }
}
/** package-private for testing - call this from your test instead */
String getAuthenticationService(AuthenticationService service) {
    try {
        Authentication endpoint = service.getAuthenticationPort();
        String token = endpoint.authenticate(username, password);
           return token;    
    } catch (Exception e) {
        throw new GenericException(
                "OpenText AuthenticationService not working Error is "
                            + e.toString());
    }
}

现在,您可以将mockService传递到getAuthenticationService(service),并且您的代码将使用您的模拟,而不是它内联创建的SoapService

作为替代方案,您也可以通过包装 SoapService 构造函数来给自己一个接缝:

/** overridden in tests */
protected AuthenticationService createSoapService(String url, QName qname) {
    return new SoapService(url, qname);
}
public String getAuthenticationService() {
    try {
        URL wsdlURL = new URL(authenticationURL);
        SoapService service = createSoapService(wsdlURL,
            new QName("SomeQName", "SoapService"));
        Authentication endpoint = service.getAuthenticationPort();
        String token = endpoint.authenticate(username, password);
           return token;    
    } catch (Exception e) {
        throw new GenericException(
                "OpenText AuthenticationService not working Error is "
                            + e.toString());
    }
}
// in your test:
SystemUnderTest yourSystem = new YourSystem() {
  @Override protected AuthenticationService createAuthenticationService(
      String url, QName qname) {
    return mockService;
  }
}

相关内容

  • 没有找到相关文章