`Mockito.mockConstruction` 在被测试的类中不返回模拟对象。

huangapple go评论96阅读模式
英文:

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&lt;AdOptions&gt; adOptionsStaticMocked = Mockito.mockStatic(AdOptions.class)) {
            adOptionsStaticMocked.when(AdOptions::getFactory).thenReturn(adOptionsFactoryMock);
            
            try (MockedConstruction&lt;AdView&gt; 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&lt;AdOptions&gt; adOptionsStaticMocked = Mockito.mockStatic(AdOptions.class)) {
            adOptionsStaticMocked.when(AdOptions::getFactory).thenReturn(adOptionsFactoryMock);
            
            try (MockedConstruction&lt;AdView&gt; 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&lt;AdOptions&gt; adOptionsStaticMocked = Mockito.mockStatic(AdOptions.class)) {
            adOptionsStaticMocked.when(AdOptions::getFactory).thenReturn(adOptionsFactoryMock);
            
            try (MockedConstruction&lt;AdView&gt; adViewMocked = Mockito.mockConstruction(AdView.class)) {
                Context mockedContext = mock(Context.class);
                AdView adView = new AdView(mockedContext);


                sut.showBanner(pluginCallMock);
            }
        }
    }

huangapple
  • 本文由 发表于 2020年9月30日 23:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64140926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定