Currently it's possible that mocked void methods could be called with the wrong parameters but the test would still pass if no mock setups/verifications have been added.
I think that if a mocked void method is called (with strict mode turned on) then Mockito should fail the test if it does not contain at least one of the following:
- An explicit doNothing() setup call at the start of the test for each mocked void method that was called.
- An explicit verify() call at the end of the test for each mocked void method that was called.
For example, If I have the following code:
public Result doSomeAction(Request request) {
someExternalClass.someVoidMethod(request);
Result result = new Result();
// setSomeStuff on Result object
return result;
}
And I have the following test for that code:
@Test
void doSomeActionSuccess() {
Request testRequest = setupTestRequest();
Result result =
classUnderTest.doSomeAction(testRequest);
// Some assertions here
}
Then this test will pass even though I haven't verified that the mocked 'someExternalClass.someVoidMethod()' was called with the correct parameters.
This isn't usually as much of a concern for mocked non-void methods because if they are called without a setup then they will return null which has a greater chance of causing problems during the test run that a developer would spot and fix (although that's by no means guaranteed).
I think that if void methods are called (with strict stubbing turned on), like the example above then it should require the test to contain at least one of the following:
- doNothing().when(someExternalClass).someVoidMethod(testRequest);
- verify(someExternalClass).someVoidMethod(testRequest);