我使用Mockito
和Roboelectric
进行测试。
我在方法中遇到了泛型参数的错误,需要测试:
通缉但未调用: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
}
}
可能是什么问题?
答案 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);
}