2017-06-12 28 views
1

我使用MockitoRoboelectric進行測試。 我面對錯誤與方法通用參數,它需要的測試:Mockito通用參數

通緝但未被調用:splashViewState.startActivity( 類ru.techmas.androidtemplate.activities.MainActivity);

SplashPresenter:

@InjectViewState 
public class SplashPresenter extends BasePresenter<SplashView> { 

    @Inject 
    SplashPresenter(RestApi restApi, TokenHelper preferenceHelper) { 
     this.restApi = restApi; 
     this.tokenHelper = preferenceHelper; 
     startNext(); 
    } 


    public final void startNext() { 
     getViewState().showErrorConnection(false); 
     if (tokenHelper.isFirstRun()) { 
      getViewState().startActivity(MainActivity.class); 
     } 
    } 

} 

SplashPresenterTest:

@RunWith(RobolectricTestRunner.class) 
public class SplashPresenterTest { 
    @Mock 
    SplashView splashView; 
    @Mock 
    SplashView$$State splashViewState; 
    @Mock 
    RestApi restApi; 

    @Mock 
    TokenHelper tokenHelper; 

    private SplashPresenter splashPresenter; 

    @Before 
    public void setUp() throws Exception { 
     MockitoAnnotations.initMocks(this); 
     splashPresenter = new SplashPresenter(restApi, tokenHelper); 
     splashPresenter.attachView(splashView); 
     splashPresenter.setViewState(splashViewState); 
    } 


    @Test 
    public void startNextTest() { 
     splashPresenter.startNext(); 
     verify(splashViewState).showErrorConnection(false); 
     when(tokenHelper.isFirstRun()).thenReturn(true); 
     verify(splashViewState).startActivity(MainActivity.class);//error here 
    } 

} 

可能是什麼問題呢?

回答

1

這很容易,而且沒有什麼關於Robolectric

修改您的測試下一個:

@Test 
    public void startNextTest() { 
     //given 
     when(tokenHelper.isFirstRun()).thenReturn(true); 
     //when 
     splashPresenter.startNext(); 
     //then 
     verify(splashViewState).showErrorConnection(false); 
     verify(splashViewState).startActivity(MainActivity.class); 
    } 
+0

謝謝你,它的工作! –

+0

不客氣。很高興它解決了! –