New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Verify on Spy Interface incorrect count of actual invocation #2915
Comments
I have the same issue on upgrade. I believe the issue is using |
Indeed removing the spy() made the test pass. Still the current message feels wrong, an clear error indicating that spying a mock is forbidden would be better. |
Spying of mocks is not supported. If you would like to improve the messaging here, I would be happy to review a PR for it. You will need to update mockito/src/main/java/org/mockito/Mockito.java Line 2169 in 5486617
isMock :
|
Can someone help me understand why this only happens with interfaces and not with implementations? E.g. This breaks as @bmaggi described: List<Integer> spy = Mockito.spy(Mockito.mock(List.class));
spy.add(1);
verify(spy).add(1); This seems to work though: List<Integer> spy = Mockito.spy(Mockito.mock(LinkedList.class));
spy.add(1);
verify(spy).add(1); What makes me think that if we add a validation to the spy(T Object) method, we might break some previously working code like the one in the second example. |
@xgrimauu Indeed it's a good point, if an exception is raised it will break your second example (that is currently working) For the moment, I see these options:
@TimvdLippe What would be the best option ? |
I implemented a draft here Only raise exception on interface. Any suggestion ? |
For information : Following the comment in the PR, I changed the code to forbid all mock. |
Hi,
At first, thanks for this awesome library.
During a migration from mockito 4.4.0 to mockito 5.1.1 one of my test began to fail.
It reported that the spied mock has 2 call one a method instead of one.
I figured that it has something to do with the new default mockmaker since it's working with mockito-subclass.
Still, the message is misleading : it's suggesting that there is an error while the code works as expected.
Either there is a bug in the spy count , either the message should be changed to reflect that verify/spy on interface is impossible.
(I may also have missed something :p )
Short failing example java 11/ junit 5/ mockito 5.1.1
The text was updated successfully, but these errors were encountered: