2017-06-01 56 views
0

我有一個的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) 

而且我的適配器業已實施的單擊事件。

回答

1

當你這樣做:

RecyclerView rcv = Mockito.mock(RecyclerView.class); 

rcv成爲模擬(即返回null一個對象(或0,或false)默認情況下,任何方法調用)。所以rcv.getChildAt(0)默認情況下將返回null,當你嘗試調用它performClick(),在NullPointerException發生。

當你創建一個模擬,並希望一些方法返回一個特定的值,則必須使用Mockito.when()告訴它。所以你的情況,你應該做這樣的事情:

// configure "when" behaviour **before** calling the getChildAt() method 
Mockito.when(rcv.getChildAt(0)).thenReturn(objectYouWantToBeReturned); 

很顯然,你需要使用when之前創建objectYouWantToBeReturned。上述 代碼將使rcv.getChildAt(0)回到你想要的對象,但是隻有當你與參數0調用它。

如果你想返回相同的對象的任何值(不僅0),你可以這樣做:

Mockito.when(rcv.getChildAt(Mockito.anyInt())).thenReturn(objectYouWantToBeReturned); 

這將返回objectYouWantToBeReturnedgetChildAt所有呼叫,無論什麼參數值。

+1

謝謝@Hugo,奏效!只有與點擊有問題,從來沒有打電話的事件,或者我沒有形式來驗證。 –

+0

不客氣,高興地幫忙! – 2017-06-02 16:25:50