android - 当进行 retrofit2 请求时,单元测试 Call 对象为 null

标签 android unit-testing mockito robolectric retrofit2

早些时候,我的项目使用的是 Retrofit,当我编写单元测试时,一切似乎都运行良好。现在我们使用 compile 'com.squareup.retrofit2:retrofit:2.0.0' 升级到 Retrofit 2.0。 我们正在使用 Robolectric 进行单元测试。 这是我们为早期 Retrofit 版本编写的测试。

        @Test
        public void testButton() throws Exception {
            button.setText("www.google.com");            
            button.performClick();    
            Mockito.verify(mockApiservice).getLinkDetails(requestArgumentCaptor.capture());
            assertThat(requestArgumentCaptor.getValue()).isEqualTo("www.google.com");         
            assertThat(button.getVisibility()).isEqualTo(View.GONE);
        }

现在当我们升级到Retrofit 2.0时,按钮点击真正的代码变为

 Call<MyPOJO> callObj = ApiService.getLinkDetails(encodedUrlString);
 callObj.enqueue(this);

即使我将测试代码更改为 - 测试仍然失败

            @Test
            public void testButton() throws Exception {
                button.setText("www.google.com");            
                button.performClick();         
                assertThat(button.getVisibility()).isEqualTo(View.GONE);
            }

我现在得到的错误是 -

java.lang.NullPointerException at callObj.enqueue(this);

这意味着 callObj 为空 所以 2 个最重要的问题 -

  1. 为什么我在真实代码中得到 NPE?

  2. 如何更改我的测试代码以向 API 发出模拟请求并获得模拟响应?

我到处搜索,但没有找到任何线索。 请帮我。非常感谢任何输入。

日志-

java.lang.NullPointerException
    at com.myapp.MyFragment.onButtonClick(MyFragment.java:428)
    at com.myapp.fragment.MyFragment.access$100(MyFragment.java:62)
    at com.myapp.fragment.MyFragment$3.onClick(MyFragment.java:148)
    at android.view.View.performClick(View.java:4756)
    at com.myapp.MyFragmentTest.testButton(MyFragmentTest.java:122)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

最佳答案

我可能已经弄清楚了 - 看起来您正在将 getLinkDetails 作为静态方法调用,但您需要这样做:

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://url.com/")
.build();

ApiService service = retrofit.create(ApiService.class);
Call<MyPOJO> callObj = service.getLinkDetails(encodedUrlString);
callObj.enqueue(this);

您可以重用 Retrofit 对象和服务对象,但不能重用 Call

改造 2 文档:http://square.github.io/retrofit/

关于android - 当进行 retrofit2 请求时,单元测试 Call 对象为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37356605/

相关文章:

android - 在 Android 设备上调用以前的布局?

java - 使用展开/折叠动画后,我在相对布局内有一些 View ,高度保持不变,但我想根据java代码进行更改

java - Android 相机应用拍照后延迟

java - Android:无法在文件中保存超过 195 的数字

unit-testing - 是否有与 NUnit 的 testcase 属性等效的 JUnit?

java - PowerMock Mockito 方法中的模拟类构造函数

android - Robolectric 单元测试因 Android Studio 2.3 更新而失败

java - Mockito 接口(interface)方法冲突

java - 使用 Mockito、TestNG 和 OpenEJB 对 EJB 进行单元测试

mocking - PowerMockMockito 测试在 Junit 下运行但不在 TestNG 下运行