junit - Mocking HTTPSURLConnection class throws java.lang.AbstractMethodError -
i want mock 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 above code updated test class using powermockito 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 error java.lang.nullpointerexception @ last line.
can 1 suggest how fix ?
i've made test java 7, junit 4.12, mockito 1.10.19 , powermock 1.6.3
i created class code want 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 created class foo
according comments above:
public class foo { // i'm returning something, not sure how implementation (and doesn't make difference because you'll mock anyway) public static sslcontext getsslcontext() { try { return sslcontext.getdefault(); } catch (nosuchalgorithmexception e) { return null; } } // i'm returning something, not sure how implementation (and doesn't make difference because you'll mock anyway) public static hostnameverifier gethostnameverifier() { return new hostnameverifier() { @override public boolean verify(string arg0, sslsession arg1) { return true; } }; } }
as want mock code in processremotetolocal
method, you'll need mock following:
url
constructorfoo
methods:getsslcontext()
,gethostnameverifier()
url.openconnection()
method
so test class this:
// runwith needed 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 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 return mocks created above powermockito.mockstatic(foo.class); mockito.when(foo.gethostnameverifier()).thenreturn(hnamemock); mockito.when(foo.getsslcontext()).thenreturn(context); // call static method, code use mocked objects // foo static methods return mocked sslcontext , hostnameverifier created above // url.openconnecton return mocked httpsurlconnection httptest.processremotetolocal(url, "/test.out"); // assertions need } }
Comments
Post a Comment