Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 545
Kaloyan Roussev Avatar asked Mar 18 '16 10:03

Kaloyan Roussev


People also ask

What is the difference between Mockito and Robolectric?

Mockito and Robolectric can be primarily classified as "Testing Frameworks" tools. Mockito and Robolectric are both open source tools. It seems that Mockito with 9.17K GitHub stars and 1.63K forks on GitHub has more adoption than Robolectric with 4.63K GitHub stars and 1.19K GitHub forks.

Can we use JUnit without Mockito?

In other words: you can definitely use JUnit without using a mocking framework. Same is true for the reverse direction; but in reality, there are not many good reasons why you would want to use Mockito for anything else but unit testing.

What is Mockito and mock?

Mockito is a java based mocking framework, used in conjunction with other testing frameworks such as JUnit and TestNG. It internally uses Java Reflection API and allows to create objects of a service. A mock object returns a dummy data and avoids external dependencies.

Is Mockito used for unit testing?

Mockito is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing.


1 Answers

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() {   MockitoAnnotations.initMocks(this); }  @After public void tearDownMockito() {   Mockito.validateMockitoUsage(); } 
like image 175
Jeff Bowman Avatar answered Nov 07 '22 04:11

Jeff Bowman