我有一個的Mockito測試從我RecyclerView
點擊,而是拋出一個錯誤。測試的Mockito點擊RecyclerView錯誤
這是我的代碼:
@Test
public void getUserClick() {
RecyclerView rcv = Mockito.mock(RecyclerView.class);
List<User> usersList = new ArrayList<>();
User commonUser = Mockito.mock(User.class);
commonUser.setId("1");
commonUser.setName("User");
commonUser.setEmail("[email protected]");
usersList.add(commonUser);
UserAdapterTest adapter = Mockito.mock(UserAdapterTest.class);
adapter.addData(usersList);
rcv.setAdapter(adapter);
// Test Click
rcv.getChildAt(0).performClick();
//Verify Click Item
Mockito.verify(adapter).testClickItem(commonUser);
}
和錯誤推出位置:
顯示java.lang.NullPointerException在rcv.getChildAt(0).performClick();
但是IDE不告訴我別的。
java.lang.NullPointerException
at com.project.test.TestRecycler.getUserClick(TestRecycler.java:48)
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.mockito.internal.junit.JUnitRule$1.evaluateSafely(JUnitRule.java:63)
at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:43)
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.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)
而且我的適配器業已實施的單擊事件。
謝謝@Hugo,奏效!只有與點擊有問題,從來沒有打電話的事件,或者我沒有形式來驗證。 –
不客氣,高興地幫忙! – 2017-06-02 16:25:50