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

Exception or error:

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

I have this code:

public class LoginPresenterTest {

    LoginPresenter loginPresenter;

    private LoginView loginView;

    void initLoginPresenter() {

    public void whenUserNameIsEmptyShowErrorOnLoginClicked() throws Exception {

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


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

How to solve:

Use the Robolectric runner. Robolectric uses its own classloader that favors its replacements for the Android API, so it really does need to handle classloading on its own. There’s no other way to use Robolecric.

There are several ways to initialize Mockito (see this SO answer), that are strictly equivalent to using the runner, but the two that make the most sense for your case are:

  • Using MockitoRule, because you’re already on JUnit4:

    @Rule public MockitoRule rule = MockitoJUnit.rule();
  • Creating manual @Before and @After methods:

    @Before public void setUpMockito() {
    @After public void tearDownMockito() {

Leave a Reply

Your email address will not be published. Required fields are marked *