JUnit失败测试:预期:<sun.security.ssl.sslsocketfactoryimpl@48030df0>但是:<sun.security.ssl.sslsocketfactoryimpl@40882c7f> </sun.security.ssl.sslsocketfactoryimpl@40882c7f> </sun.security.ssl .sslsocketfactoryimpl @ 48030df0>

时间:2014-10-07 16:22:00

标签: unit-testing junit mockito

运行单元测试(assertEquals)时出现此错误:

java.lang.AssertionError:expected:sun.security.ssl.SSLSocketFactoryImpl@48030df0>但是:sun.security.ssl.SSLSocketFactoryImpl@40882c7f>

当我为此代码运行单元测试以测试getSslSocketFactory方法

public ClientCertSocketFactory() throws IOException{
    String trustStoreFilePath = System.getProperty("javax.net.ssl.trustStore");
    try {
        TrustManagerFactory trustManagerFactory = CertManagerFactory.loadTrustStore(trustStoreFilePath);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
        this.sslSocketFactory = sslContext.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("No Provider supports a TrustManagerFactorySpi implementation for the TLS protocol. Error message: " + e);
    } catch (KeyManagementException e) {
        LOGGER.error("Error occurred when initializing SSLContext. Error message: " + e);
    }
}

public SSLSocketFactory getSslSocketFactory() {
    return sslSocketFactory;
}

我对getSslSocketFactory的单元测试是:

@Test
public void shouldGetSslSocketFactory() throws IOException, NoSuchAlgorithmException, KeyManagementException{
    String trustStoreFilePath = System.getProperty("javax.net.ssl.trustStore");

    TrustManagerFactory trustManagerFactory = CertManagerFactory.loadTrustStore(trustStoreFilePath);
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
    this.sslSocketFactory = sslContext.getSocketFactory();

    assertEquals(sslSocketFactory, clientCertSocketFactory.getSslSocketFactory());


}

我该如何解决? 非常感谢

0 个答案:

没有答案