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?
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.
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.
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.
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With