In the usual mocking with @Mock
and @InjectMocks
annotations, the class under testing should be run with @RunWith(MockitoJUnitRunner.class)
.
@RunWith(MockitoJUnitRunner.class)
public class ReportServiceImplTestMockito {
@Mock
private TaskService mockTaskService;
@InjectMocks
private ReportServiceImpl service;
// Some tests
}
but in some example I am seeing @RunWith(PowerMockRunner.class)
being used:
@RunWith(PowerMockRunner.class)
public class Tests {
@Mock
private ISomething mockedSomething;
@Test
public void test1() {
// Is the value of mockedSomething here
}
@Test
public void test2() {
// Is a new value of mockedSomething here
}
}
could someone point it out whats the difference and when I want to use one instead of another?
On a first glance, the answer is simply: well, there are several mocking frameworks out there, and there are different ways to use them.
The first example tells JUnit to use the "unit test runner" that the Mockito mocking framework provides. The second example uses the unit test runner from the PowerMock framework.
In order for things to make sense, you would also have different import statements, as both frameworks have different implementations for the @Mock annotation for example.
( the main point of using these framework-specific test runners is that they take care of initializing all the fields with special framework-specific annotations ).
So: the difference here is simply that: the first example is written to use the Mockito framework, the second one uses PowerMock.
Now, which one of those to use?
Answer: Mockito.
Why? Somehow an ugly truth is: the PowerMock-one basically is a cry for help. It says "the class under test is badly designed, please fix it". Meaning: as a developer, you can write "easy to test" code, or "hard to test" code. Many people do the second: they write code that is hard to test. And then, PowerMock(ito) provides means to still test that code.
PowerMock(ito) gives you the ability to mock (thus control) calls to static methods, and to new()
. To enable that, PowerMock(ito) manipulates the byte code of your code under test. That is perfectly fine for small code bases, but when you face millions of lines of production code, and thousands of unit tests, things are totally different.
I have seen many PowerMock tests fail for no apparent reason, to find out hours later ... that some "static" thing somewhere else was changed, and that somehow affect a different PowerMock static/new driven test case.
At some point, our team made a conscious decision: when you write new code, and you can only test that with PowerMock ... that isn't acceptable. Since then, we only created Mockito test cases, and not once since then we saw similar bizarre problems that bugged us with PowerMock.
The only acceptable reason to use PowerMock is when you want to test existing (maybe 3rd party) code that you do not want to modify. But of course, what is the point of testing such code? When you can't modify that code, why should tests fail all of a sudden?