I have a fairly involved test case I am trying to add the following verify() to:
verify(userService).getUserById(anyLong()).setPasswordChangeRequired(eq(Boolean.TRUE));
This fails with this error:
org.mockito.exceptions.verification.TooManyActualInvocations:
userService.getUserById(<any>);
Wanted 1 time:
-> at test.controllers.AuthenticationControllerMockTest.testLookupsExceeded(AuthenticationControllerMockTest.java:404)
But was 4 times. Undesired invocation:
So I changed it to this:
verify(userService, atLeastOnce()).getUserById(anyLong()).setPasswordChangeRequired(eq(Boolean.TRUE));
And now it fails with:
java.lang.NullPointerException
at test.controllers.AuthenticationControllerMockTest.testLookupsExceeded(AuthenticationControllerMockTest.java:404)
because this is returning null:
verify(userService, atLeastOnce()).getUserById(anyLong())
This seems puzzling – If I use the default (one invocation only), it fails because it’s being invoked multiple times, but if I tell it that multiple invocations are OK, it fails because it can’t find any invocations!
Can anyone help with this?
It looks like you both want to mock what happens when
userService.getUserById()is called, and also verify thatsetPasswordChangeRequired(true)is called on that returned object.You can accomplish this with something like: