user3789902
user3789902

Reputation: 123

Mocking HTTPSURLConnection class throws java.lang.AbstractMethodError

I want to mock the following lines

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();

    }

For the above code I updated my test class using PowerMockito as below

@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);

This gives the error java.lang.NullPointerException at the last line.

Can some one suggest how to fix it ?

Upvotes: 0

Views: 1741

Answers (1)

user7605325
user7605325

Reputation:

I've made a test with java 7, JUnit 4.12, Mockito 1.10.19 and PowerMock 1.6.3

I created a class with the code you want to mock:

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

I also created class Foo according to your comments above:

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

As you want to mock the code in processRemoteToLocal method, you'll need to mock only the following:

  • URL constructor
  • Foo methods: getSslContext() and getHostnameVerifier()
  • URL.openConnection() method

So your test class will be something like this:

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

Upvotes: 1

Related Questions