I am trying to write missing tests for an capacitor android plugin written in Java that uses google's AdMob lib.
The function I am trying to testing has a new AdView() call that is returning null even when the constructor is mocked with Mockito's object construction mock and I am not quite sure why.
I try to create a new AdView inside the test threat and it works.
This is the current test (it does not have any assert yet because I am just trying to pass the view creation:
@Test
public void npa_requests_for_banner_when_specified_inside_options() {
sut = new AdMob();
try (MockedStatic<AdOptions> adOptionsStaticMocked = Mockito.mockStatic(AdOptions.class)) {
adOptionsStaticMocked.when(AdOptions::getFactory).thenReturn(adOptionsFactoryMock);
try (MockedConstruction<AdView> adViewMocked = Mockito.mockConstruction(AdView.class)) {
Context mockedContext = mock(Context.class);
AdView adView = new AdView(mockedContext);
sut.showBanner(pluginCallMock);
}
}
}
and the showBanner relevant code:
@PluginMethod
public void showBanner(PluginCall call) {
if (mAdView != null) {
return;
}
final AdOptions adOptions = AdOptions
.getFactory()
.createBannerOptions(call);
setTestDevicesIfNeeded(adOptions.isTesting);
try {
mAdView = new AdView(getContext()); // THIS BREAKS! NO MOCK IS RETURNED
mAdView.setAdSize(adOptions.adSize.size);
mAdViewLayout = new RelativeLayout(getContext());
mAdViewLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
mAdViewLayout.setVerticalGravity(Gravity.BOTTOM);
// More Code here...
} catch (Exception ex) {
call.error(ex.getLocalizedMessage(), ex);
}
}
The mAdView = new AdView(getContext()) part is where the code breaks.
Does anyone can tell me what I am doing wrong?