Mockito 测试 EpoxyController 在实例化时抛出 NPE

Mockito testing EpoxyController throws NPE on instantiation

我使用 Airbnb 的 Epoxy 2.0,我想对我的 Controller 进行单元测试。每当我尝试使用 Mockito 实例化一个新的 Controller 时,它都会给我一个 NPE。

也许沿着 Mockito 走是这里的错误路线。除了 Mockito、Espresso 和 JUnit 之外,我从未使用过任何东西——也许我应该研究一下 Robolectric/Android AssertJ?

private MainController controller;
@Mock Context context;
@Mock MainContract.View view;
@Mock SharedPrefsManager sharedPrefsManager;
@Mock ImageViewAnimator imageViewAnimator;
@Mock DateFormatter dateFormatter;

@Before
public void setup()
{
    MockitoAnnotations.initMocks(this);
    controller = new MainController(context, view, sharedPrefsManager, imageViewAnimator, dateFormatter); //throws on this line
}

堆栈跟踪:

java.lang.NullPointerException
    at android.support.v7.widget.RecyclerView$AdapterDataObservable.hasObservers(RecyclerView.java:11350)
    at android.support.v7.widget.RecyclerView$Adapter.hasObservers(RecyclerView.java:6549)
    at android.support.v7.widget.RecyclerView$Adapter.setHasStableIds(RecyclerView.java:6423)
    at com.airbnb.epoxy.BaseEpoxyAdapter.<init>(BaseEpoxyAdapter.java:50)
    at com.airbnb.epoxy.EpoxyControllerAdapter.<init>(EpoxyControllerAdapter.java:17)
    at com.airbnb.epoxy.EpoxyController.<init>(EpoxyController.java:40)
    at bj.rxjavaexperimentation.main.MainController.<init>(MainController.java:50)
    at bj.rxjavaexperimentation.main.MainControllerUnitTest.setup(MainControllerUnitTest.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.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.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:68)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:74)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

将 Roboletric 与 Mockito 结合使用有效并且不会抛出 NPE。