java - How can I mock Google's Geocoding API request using mockito/powermock? -
i want unit test method using mockito/powermock:
@service public class googleapiservice { private static final logger logger = loggerfactory.getlogger(googleapiservice.class); private static final string language = "es"; private list<addresstype> addresstypes = arrays.aslist( addresstype.locality, addresstype.administrative_area_level_2, addresstype.administrative_area_level_1, addresstype.country ); @autowired private geoapicontext geoapicontext; public string getlocalityfromlatlng(latlng latlng) throws exception { logger.debug("getlocalityfromlatlng"); geocodingresult[] geocodingresults = geocodingapi.newrequest(geoapicontext) .latlng(latlng) .await(); (geocodingresult geocodingresult : geocodingresults) { addresstype addresstype = geocodingresult.types[0]; if (addresstypes.contains(addresstype)) { return geocodingresult.formattedaddress; } } return stringutils.empty; } }
this have tried:
@preparefortest(geocodingapi.class) public class googleapiserviceunittest extends abstractunittest { private static final double latitude = -38.010403; private static final double longitude = -57.558408; @mock private geoapicontext geoapicontext; @injectmocks private googleapiservice googleapiservice; @test public void testgetlocalityfromlatlng() throws exception { latlng latlng = new latlng(latitude, longitude); geocodingresult geocodingresult = new geocodingresult(); geocodingresult[] geocodingresults = new geocodingresult[] { geocodingresult }; geocodingapirequest geocodingapirequest = new geocodingapirequest(geoapicontext); geocodingapirequest.latlng(latlng); mockstatic(geocodingapi.class); // when(geocodingapi.newrequest(geoapicontext)) // .thenreturn(geocodingapirequest); // when(geocodingapi.newrequest(geoapicontext).latlng(latlng)) // .thenreturn(geocodingapirequest); when(geocodingapi.newrequest(geoapicontext).latlng(latlng).await()) .thenreturn(geocodingresults); string locality = googleapiservice.getlocalityfromlatlng(latlng); assertthat(locality, is(notnullvalue())); verifystatic(times(1)); geocodingapi.newrequest(geoapicontext).latlng(latlng).await(); verifynomoreinteractions(geoapicontext); } }
i'm getting nullpointerexception. here stack trace:
java.lang.nullpointerexception @ com.google.maps.pendingresultbase.await(pendingresultbase.java:56) @ com.beermap.server.unit.service.googleapiserviceunittest.testgetlocalityfromlatlng(googleapiserviceunittest.java:57) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ org.junit.internal.runners.testmethod.invoke(testmethod.java:68) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runtestmethod(powermockjunit44runnerdelegateimpl.java:310) @ org.junit.internal.runners.methodroadie$2.run(methodroadie.java:89) @ org.junit.internal.runners.methodroadie.runbeforesthentestthenafters(methodroadie.java:97) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.executetest(powermockjunit44runnerdelegateimpl.java:294) @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.executetestinsuper(powermockjunit47runnerdelegateimpl.java:127) @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.executetest(powermockjunit47runnerdelegateimpl.java:82) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runbeforesthentestthenafters(powermockjunit44runnerdelegateimpl.java:282) @ org.junit.internal.runners.methodroadie.runtest(methodroadie.java:87) @ org.junit.internal.runners.methodroadie.run(methodroadie.java:50) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.invoketestmethod(powermockjunit44runnerdelegateimpl.java:207) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.runmethods(powermockjunit44runnerdelegateimpl.java:146) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$1.run(powermockjunit44runnerdelegateimpl.java:120) @ org.junit.internal.runners.classroadie.rununprotected(classroadie.java:34) @ org.junit.internal.runners.classroadie.runprotected(classroadie.java:44) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.run(powermockjunit44runnerdelegateimpl.java:122) @ org.powermock.modules.junit4.common.internal.impl.junit4testsuitechunkerimpl.run(junit4testsuitechunkerimpl.java:106) @ org.powermock.modules.junit4.common.internal.impl.abstractcommonpowermockrunner.run(abstractcommonpowermockrunner.java:53) @ org.powermock.modules.junit4.powermockrunner.run(powermockrunner.java:59) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:78) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:212) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:68) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ com.intellij.rt.execution.application.appmain.main(appmain.java:140)
is in await() call line
edit: how looks now, still not working
btw, @runwith(powermockrunner.class)
in abstractunittest
@test public void testgetlocalityfromlatlng() throws exception { latlng latlng = new latlng(latitude, longitude); geocodingresult geocodingresult = new geocodingresult(); geocodingresult.types = new addresstype[] { addresstype.locality }; geocodingresult.formattedaddress = formatted_address; geocodingresult[] geocodingresults = new geocodingresult[] { geocodingresult }; geocodingapirequest geocodingapirequest = mock(geocodingapirequest.class); mockstatic(geocodingapi.class); when(geocodingapirequest.latlng(latlng)).thenreturn(geocodingapirequest); when(geocodingapirequest.await()).thenreturn(geocodingresults); // npe here when(geocodingapi.newrequest(geoapicontext)).thenreturn(geocodingapirequest); string locality = googleapiservice.getlocalityfromlatlng(latlng); assertthat(locality, is(notnullvalue())); // verifystatic(times(1)); // geocodingapi.newrequest(geoapicontext).latlng(latlng).await(); // verifynomoreinteractions(geoapicontext); }
and stack:
java.lang.nullpointerexception @ com.google.maps.pendingresultbase.makerequest(pendingresultbase.java:79) @ com.google.maps.pendingresultbase.await(pendingresultbase.java:55) @ com.beermap.server.unit.service.googleapiserviceunittest.testgetlocalityfromlatlng(googleapiserviceunittest.java:56) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ org.junit.internal.runners.testmethod.invoke(testmethod.java:68) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runtestmethod(powermockjunit44runnerdelegateimpl.java:310) @ org.junit.internal.runners.methodroadie$2.run(methodroadie.java:89) @ org.junit.internal.runners.methodroadie.runbeforesthentestthenafters(methodroadie.java:97) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.executetest(powermockjunit44runnerdelegateimpl.java:294) @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.executetestinsuper(powermockjunit47runnerdelegateimpl.java:127) @ org.powermock.modules.junit4.internal.impl.powermockjunit47runnerdelegateimpl$powermockjunit47methodrunner.executetest(powermockjunit47runnerdelegateimpl.java:82) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$powermockjunit44methodrunner.runbeforesthentestthenafters(powermockjunit44runnerdelegateimpl.java:282) @ org.junit.internal.runners.methodroadie.runtest(methodroadie.java:87) @ org.junit.internal.runners.methodroadie.run(methodroadie.java:50) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.invoketestmethod(powermockjunit44runnerdelegateimpl.java:207) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.runmethods(powermockjunit44runnerdelegateimpl.java:146) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl$1.run(powermockjunit44runnerdelegateimpl.java:120) @ org.junit.internal.runners.classroadie.rununprotected(classroadie.java:34) @ org.junit.internal.runners.classroadie.runprotected(classroadie.java:44) @ org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateimpl.run(powermockjunit44runnerdelegateimpl.java:122) @ org.powermock.modules.junit4.common.internal.impl.junit4testsuitechunkerimpl.run(junit4testsuitechunkerimpl.java:106) @ org.powermock.modules.junit4.common.internal.impl.abstractcommonpowermockrunner.run(abstractcommonpowermockrunner.java:53) @ org.powermock.modules.junit4.powermockrunner.run(powermockrunner.java:59) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:78) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:212) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:68) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ com.intellij.rt.execution.application.appmain.main(appmain.java:140)
thanks in advance
there several mistakes here , show have misunderstanding of mocking. let analyse them:
at first, hope it's copy-past mistake, don't have @runwith(powermockrunner.class)
, otherwise you'll exception.
second point, commented out line right when(geocodingapi.newrequest(geoapicontext)).thenreturn(geocodingapirequest);
. when static method of geocodingapi
then want return geocodingapirequest
.
at last, important point, line incorrect: when(geocodingapi.newrequest(geoapicontext).latlng(latlng).await()).thenreturn(geocodingresults);
only 1 static call mocked here: geocodingapi.newrequest(geoapicontext)
.
and instance of object return, because create new instance geocodingapirequest geocodingapirequest = new geocodingapirequest(geoapicontext);
.
the latlng(latlng)
method called object. , it's call, not mocked. seems me want mock it.
then let's mock it: geocodingapirequest geocodingapirequest = mock(geocodingapirequest.class);
after. mock calls need mocked:
when(geocodingapirequest.latlng(latlng)).thenreturn(geocodingapirequest); when(geocodingapirequest.await()).thenreturn(geocodingresults);
another mistake, important previous , more obviously: required files isn't set geocodingresult
.
geocodingresult geocodingresult = new geocodingresult(); geocodingresult.types = new addresstype[]{ addresstype.locality}; geocodingresult.formattedaddress = "some address";
the full working test:
@runwith(powermockrunner.class) @preparefortest({geocodingapi.class, geocodingapirequest.class}) public class googleapiserviceunittest { private static final double latitude = -38.010403; private static final double longitude = -57.558408; @mock private geoapicontext geoapicontext; @injectmocks private googleapiservice googleapiservice; @test public void testgetlocalityfromlatlng() throws exception { latlng latlng = new latlng(latitude, longitude); geocodingresult geocodingresult = new geocodingresult(); geocodingresult.types = new addresstype[]{ addresstype.locality}; geocodingresult.formattedaddress = "some address"; geocodingresult[] geocodingresults = new geocodingresult[] { geocodingresult }; geocodingapirequest geocodingapirequest = mock(geocodingapirequest.class); when(geocodingapirequest.latlng(latlng)).thenreturn(geocodingapirequest); when(geocodingapirequest.await()).thenreturn(geocodingresults); mockstatic(geocodingapi.class); when(geocodingapi.newrequest(eq(geoapicontext))) .thenreturn(geocodingapirequest); string locality = googleapiservice.getlocalityfromlatlng(latlng); assertthat(locality, is(notnullvalue())); verifystatic(times(1)); geocodingapi.newrequest(geoapicontext); // add verification other mock if need verifynomoreinteractions(geoapicontext); } }
Comments
Post a Comment