使用Mockito进行单元测试:验证失败测试(模拟).method()

时间:2014-10-11 17:14:44

标签: mockito

我想为createSocket(String host,int port)方法编写单元测试,但是我有这个错误消息:

shouldCreateSocketWithHostAndPort(.... ClientCertSocketFactoryTest)org.mockito.exceptions.misusing.NotAMockException:传递给verify()的参数是SSLSocketFactoryImpl类型,不是模拟!确保正确放置括号!请参阅正确验证的示例:verify(mock).someMethod();

public SSLSocketFactory getSslSocketFactory() {
    return sslSocketFactory;
}

public Socket createSocket(String host, int port)
        throws IOException {
    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    return sslSocket;
}

这是我写的测试:

@Before
public void setUp() throws Exception {
sslSocketFactory = mock(SSLSocketFactory.class);
clientCertSocketFactory = new ClientCertSocketFactory();
sslSocketFactory = clientCertSocketFactory.getSslSocketFactory();   
}

@Test
public void shouldCreateSocketWithHostAndPort() throws Exception {
clientCertSocketFactory.createSocket(HOST, PORT);
verify(sslSocketFactory).createSocket(HOST, PORT);
}

如何修复此错误。感谢

1 个答案:

答案 0 :(得分:0)

此代码全部错误。

模拟的ssl套接字工厂从未使用过 - 它被创建,然后被getSslSocketFactory的调用所取代。