2017-05-01 53 views
0

我使用Airbnb的Epoxy 2.0,我想單元測試我Controller。每當我試圖實例化一個新Controller使用它的Mockito給我NPE。測試的Mockito拋出EpoxyController NPE上實例

或許下去是的Mockito這裏走錯路線。我從未使用過任何東西,除了的Mockito,咖啡和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

使用Roboletric與工作的Mockito,不拋出NPE。