Mockito泛型论证

时间:2017-06-12 15:00:36

标签: mockito

我使用MockitoRoboelectric进行测试。 我在方法中遇到了泛型参数的错误,需要测试:

  

通缉但未调用:splashViewState.startActivity(       class 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 个答案:

答案 0 :(得分: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);
    }