2017-06-12 15 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

ようこそ。うれしいことが解決されました! –