I need both Robolectric and Mockito in my test, each one proposes their own TestRunner

Viewed 6437

I need both Robolectric and Mockito in my test, each one proposes their own TestRunner, what do I do?

I have this code:

@RunWith(MockitoJUnitRunner.class)
@EBean
public class LoginPresenterTest {

    @Bean
    LoginPresenter loginPresenter;

    @Mock
    private LoginView loginView;

    @AfterInject
    void initLoginPresenter() {
        loginPresenter.setLoginView(loginView);
    }

    @Test
    public void whenUserNameIsEmptyShowErrorOnLoginClicked() throws Exception {
        when(loginView.getUserName()).thenReturn("");
        when(loginView.getPassword()).thenReturn("asdasd");
        loginPresenter.onLoginClicked();
        verify(loginView).setEmailFieldErrorMessage();
    }
}

The problem is AndroidAnnotations doesn't inject the dependencies and I get a NPE when trying to use LoginPresenter

Someone told me to use the LoginPresenter_ constructor, so that I can force inject the dependency myself this way:

LoginPresenter loginPresenter = LoginPresenter_.getInstance_(context);

In order to gain access to the context, I had to switch from Unit Tests to Android Instrumentation Tests and do getInstrumentation().getTargetContext() but I want Unit Tests, and not Instrumentation ones.

So another person told me to use Robolectric for that - it is supposed to be able to provide me with an Application context.

However, when I looked at the getting started page of Robolectric, it says

@RunWith(RobolectricGradleTestRunner.class)

which is going to clash with my current @RunWith annotation for Mockito, so what do I do?

1 Answers
Related