我正在为MVP安卓应用中的视图编写测试。在这些测试中,我使用的是Robolectric 3.6.1。我遇到的问题是当我尝试使用getChildAt()
单击RecyclerView内的视图时获取NullPointerException。这只发生在整个测试类运行时,而不是在单独运行测试时,这让我觉得我的设置中的某些内容是错误的。如果我运行整个类,第一个测试没有这个问题,但所有其他测试都没有,所以当setUp()
函数被多次调用时,似乎会出现问题。
我做了一些研究并发现了this帖子,但是把它放在我的代码中并没有用。
@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {
private List<Measurement> measurements = Arrays.asList(new Measurement("Object A", Unit.CM), new Measurement("Object B", Unit.KG));
private final int POSITION = 0;
@Mock
private MainMvpPresenter mainMvpPresenter;
private MainActivity mainActivity;
private RecyclerView measurementsRecyclerView;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mainActivity = Robolectric.buildActivity(MainActivity.class).create().visible().get();
mainActivity.setMainMvpPresenter(mainMvpPresenter);
measurementsRecyclerView = mainActivity.findViewById(R.id.measurementsList);
mainActivity.updateMeasurements(measurements);
}
@Test
public void setUpCorrectly(){
assertThat(measurementsRecyclerView.getChildCount(), is(measurements.size()));
}
@Test
public void onItemClicked_ShouldCallPresenter() {
measurementsRecyclerView.findViewHolderForAdapterPosition(POSITION).itemView.performClick();
verify(mainMvpPresenter).measurementClicked(POSITION);
}
}
这是我得到的错误:
java.lang.NullPointerException
at com.jakkaps.body_measurements.ui.main.MainActivityTest.onItemClicked_ShouldCallPresenter(MainActivityTest.java:64)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:535)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:249)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:123)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
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.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:77)
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:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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.AppMainV2.main(AppMainV2.java:131
)
答案 0 :(得分:0)
我没有真正找到解决实际问题的方法,我只是玩得开心直到它起作用。我必须做的是不在measurementsRecyclerView = mainActivity.findViewById(R.id.measurementsList);
函数中调用setUp()
,而是在每个测试中单独调用。我还必须两次致电mainActivity.updateMeasurements(measurements);
。
变得非常混乱,所以我建议任何人找到自己的解决方案。