模拟 HTTPSURLConnection class 抛出 java.lang.AbstractMethodError

Mocking HTTPSURLConnection class throws java.lang.AbstractMethodError

我想模拟以下几行

static void processRemoteToLocal(String srcUrl, String destFile) {

        URL fileUrl = new URL(srcUrl);

        HttpsURLConnection.setDefaultSSLSocketFactory(Foo.getSslContext().getSocketFactory());

        HttpsURLConnection.setDefaultHostnameVerifier(Foo.getHostnameVerifier());

        HttpsURLConnection connection = (HttpsURLConnection) fileUrl.openConnection();

    }

对于上面的代码,我使用 PowerMockito 更新了我的测试 class,如下所示

@Test
@PrepareForTest({Foo.class,SSLSocketFactory.class})
public void shouldSetTestMockServeField() throws Exception {

    HostnameVerifier hnameMock = PowerMockito.mock(HostnameVerifier.class);
    SSLSocketFactory mockSocFac = PowerMockito.mock(SSLSocketFactory.class);
    HttpsURLConnection huc = PowerMockito.mock(HttpsURLConnection.class);
    Foo mockCert = PowerMockito.mock(Foo.class);
    SSLContext sslMock = PowerMockito.mock(SSLContext.class);

    final SSLSocketFactory sslFac = null;

    URL u = PowerMockito.mock(URL.class);
    String url = "https://localhost";
    PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(u);

    PowerMockito.mockStatic(Foo.class);
    Mockito.when(Foo.getSslContext()).thenReturn(sslMock);
    Mockito.when(sslMock.getSocketFactory()).thenReturn(mockSocFac);

最后一行出现错误 java.lang.NullPointerException。

有人可以建议如何修复它吗?

我用 java 7、JUnit 4.12、Mockito 1.10.19 和 PowerMock 1.6.3 进行了测试

我用你想要模拟的代码创建了一个 class:

public class HttpTest {

    static void processRemoteToLocal(String srcUrl, String destFile) throws Exception {
        URL fileUrl = new URL(srcUrl);

        HttpsURLConnection.setDefaultSSLSocketFactory(Foo.getSslContext().getSocketFactory());

        HttpsURLConnection.setDefaultHostnameVerifier(Foo.getHostnameVerifier());

        HttpsURLConnection connection = (HttpsURLConnection) fileUrl.openConnection();
    }
}

我也根据您上面的评论创建了classFoo

public class Foo {
    // I'm just returning something, not sure how your implementation is (and it doesn't make difference because you'll mock it anyway)
    public static SSLContext getSslContext() {
        try {
            return SSLContext.getDefault();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

    // I'm just returning something, not sure how your implementation is (and it doesn't make difference because you'll mock it anyway)
    public static HostnameVerifier getHostnameVerifier() {
        return new HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        };
    }
}

因为你想模拟 processRemoteToLocal 方法中的代码,你只需要模拟以下内容:

  • URL构造函数
  • Foo 方法:getSslContext()getHostnameVerifier()
  • URL.openConnection()方法

所以你的测试 class 将是这样的:

// RunWith needed for powermock
@RunWith(PowerMockRunner.class)
@PrepareForTest({ HttpTest.class, Foo.class, SSLContext.class, URL.class })
public class MyTestClass {

    @Test
    public void shouldSetTestMockServeField() throws Exception {
        URL u = PowerMockito.mock(URL.class);
        String url = "https://localhost";
        // mock URL constructor
        PowerMockito.whenNew(URL.class).withArguments(Matchers.anyString()).thenReturn(u);
        // mock openConnection() method
        HttpsURLConnection huc = Mockito.mock(HttpsURLConnection.class);
        Mockito.when(u.openConnection()).thenReturn(huc);

        // create mocks for Foo class
        HostnameVerifier hnameMock = Mockito.mock(HostnameVerifier.class);
        SSLContext context = PowerMockito.mock(SSLContext.class);
        SSLSocketFactory mockSocFac = Mockito.mock(SSLSocketFactory.class);
        Mockito.when(context.getSocketFactory()).thenReturn(mockSocFac);

        // mock Foo static methods to return mocks created above
        PowerMockito.mockStatic(Foo.class);
        Mockito.when(Foo.getHostnameVerifier()).thenReturn(hnameMock);
        Mockito.when(Foo.getSslContext()).thenReturn(context);

        // call static method, code will use mocked objects
        // Foo static methods will return mocked SSLContext and HostnameVerifier created above
        // URL.openConnecton will return mocked HttpsURLConnection
        HttpTest.processRemoteToLocal(url, "/test.out");

        // do the assertions you need
    }
}