@RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this) @RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this) java java

@RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this)


MockitoJUnitRunner gives you automatic validation of framework usage, as well as an automatic initMocks().

The automatic validation of framework usage is actually worth having. It gives you better reporting if you make one of these mistakes.

  • You call the static when method, but don't complete the stubbing with a matching thenReturn, thenThrow or then. (Error 1 in the code below)

  • You call verify on a mock, but forget to provide the method callthat you are trying to verify. (Error 2 in the code below)

  • You call the when method after doReturn, doThrow ordoAnswer and pass a mock, but forget to provide the method thatyou are trying to stub. (Error 3 in the code below)

If you don't have validation of framework usage, these mistakes are not reported until the following call to a Mockito method. This might be

  • in the same test method (like error 1 below),
  • in the next test method (like error 2 below),
  • in the next test class.

If they occur in the last test that you run (like error 3 below), they won't be reported at all.

Here's how each of those types of errors might look. Assume here that JUnit runs these tests in the order they're listed here.

@Testpublic void test1() {    // ERROR 1    // This compiles and runs, but it's an invalid use of the framework because     // Mockito is still waiting to find out what it should do when myMethod is called.    // But Mockito can't report it yet, because the call to thenReturn might     // be yet to happen.    when(myMock.method1());    doSomeTestingStuff();    // ERROR 1 is reported on the following line, even though it's not the line with    // the error.    verify(myMock).method2();}@Testpublic void test2() {    doSomeTestingStuff();    // ERROR 2    // This compiles and runs, but it's an invalid use of the framework because    // Mockito doesn't know what method call to verify.  But Mockito can't report     // it yet, because the call to the method that's being verified might     // be yet to happen.    verify(myMock);}@Testpublic void test3() {    // ERROR 2 is reported on the following line, even though it's not even in     // the same test as the error.    doReturn("Hello").when(myMock).method1();    // ERROR 3    // This compiles and runs, but it's an invalid use of the framework because    // Mockito doesn't know what method call is being stubbed.  But Mockito can't     // report it yet, because the call to the method that's being stubbed might     // be yet to happen.    doReturn("World").when(myMock);    doSomeTestingStuff();     //  ERROR 3 is never reported, because there are no more Mockito calls. }

Now when I first wrote this answer more than five years ago, I wrote

So I would recommend the use of the MockitoJUnitRunner wherever possible. However, as Tomasz Nurkiewicz has correctly pointed out, you can't use it if you need another JUnit runner, such as the Spring one.

My recommendation has now changed. The Mockito team have added a new feature since I first wrote this answer. It's a JUnit rule, which performs exactly the same function as the MockitoJUnitRunner. But it's better, because it doesn't preclude the use of other runners.

Include

@Rule public MockitoRule rule = MockitoJUnit.rule();

in your test class. This initialises the mocks, and automates the framework validation; just like MockitoJUnitRunner does. But now, you can use SpringJUnit4ClassRunner or any other JUnitRunner as well. From Mockito 2.1.0 onwards, there are additional options that control exactly what kind of problems get reported.


Using runner lets you save a little bit of coding (no need for @Before method). On the other hand using a runner is sometimes not possible, i.e. when you are already using one, like SpringJUnit4ClassRunner.

That's it. It is just a matter of preference.