java - @RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this) - Stack Overflow
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 matchingthenReturn
,thenThrow
orthen
. (Error 1 in the code below)You call
verify
on a mock, but forget to provide the method call that you are trying to verify. (Error 2 in the code below)You call the
when
method afterdoReturn
,doThrow
ordoAnswer
and pass a mock, but forget to provide the method that you 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.
Read full article from java - @RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this) - Stack Overflow
No comments:
Post a Comment