Mockito Stubbing

时间:2015-04-13 17:36:46

标签: junit mockito

下面列出的是我尝试使用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服务,并且方法存根不起作用,我在这里做错了什么

1 个答案:

答案 0 :(得分:1)

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