我使用的是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$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.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$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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.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)
答案 0 :(得分:0)
将Roboletric与Mockito一起使用并且不会抛出NPE。