英文:
Mockito.mockConstruction does not return the mocked object in the tested class
问题
我正在尝试为一个使用谷歌的 AdMob 库编写的 Java Capacitor Android 插件编写缺失的测试。
我正在尝试测试的函数中有一个 new AdView()
调用,但即使构造函数被 Mockito 的对象构造模拟 所模拟,它仍然返回 null
,我不太确定为什么会这样。
我尝试在测试线程内创建一个新的 AdView
,它能够工作。
这是当前的测试(目前还没有任何断言,因为我只是在尝试通过视图创建):
@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);
}
}
}
以及与 showBanner
相关的代码:
@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()); // 这里出错了!没有返回模拟
mAdView.setAdSize(adOptions.adSize.size);
mAdViewLayout = new RelativeLayout(getContext());
mAdViewLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
mAdViewLayout.setVerticalGravity(Gravity.BOTTOM);
// 更多代码在这里...
} catch (Exception ex) {
call.error(ex.getLocalizedMessage(), ex);
}
}
代码中的 mAdView = new AdView(getContext())
部分是出错的地方。
有人可以告诉我我做错了什么吗?
英文:
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:
<!-- language-all: lang-java -->
@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?
答案1
得分: 0
这可能对像我一样在Java中刚接触Capacitor插件的新手有所帮助。
问题在于getContext()
返回了null... 我所做的是:
@Test
public void npa_requests_for_banner_when_specified_inside_options() {
sut = new AdMob() {
@Override
public Context getContext() {
return mockedContext;
}
};
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);
}
}
}
英文:
Maybe this helps someone new in Capacitor plugins in Java like me.
The problem was that getContext()
returned null... What I did was:
@Test
public void npa_requests_for_banner_when_specified_inside_options() {
sut = new AdMob() {
@Override
public Context getContext() {
return mockedContext;
}
};
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);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论