Mockito 存根

Mockito Stubbing

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

Java代码

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;
    }

Junit 方法

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;
  }
}