Skip to content
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

Spy annotation does not seem to work after switching from Mockito 4.8.1 to 5.10.0 #3271

Open
floM99 opened this issue Feb 16, 2024 · 1 comment

Comments

@floM99
Copy link

floM99 commented Feb 16, 2024

We are trying to update from Mockito 4.8.1 to 5.10.0 using Java 17. We now have Problems with the Spy annotation. It does not seem to have any effect. If we create a spy using the spy() method, the spy works just fine.

The Spy is not injected with InjectMocks. If I look in the IntelliJ debug, I can see, that the Type of the Variable is just "HashSet". With Mockito 4.8.1, the type was "HashSet$MockitoMock". Is the spy not injected properly, because it is not created correctly? It seems like it has no wrapper at all. But why does the spy() method work then? It also just says "HashSet" as type in debug mode. Did we miss something or is this actually a bug?

Does not work:

@Spy
private Set<String> strings = new HashSet<>();

@InjectMocks
private SomeClass someClass;

Works:

private Set<String> strings = Mockito.spy(new HashSet<>());

@InjectMocks
private SomeClass someClass;

4.8.1:
grafik

5.10.0:
grafik

@jkim323
Copy link

jkim323 commented Feb 24, 2024

Correct me if I am mistaken but it could be related due to the change in @SPY annotation behavior after Mockito 3.4.0 version. I believe starting from Mockito 3.4.0 version when using the annotation, Mockito won't create a wrapper object, and the spy is just a reference to the real object. So to create a wrapper object, you will have to use Mockito.spy() and that is probably why you are also seeing this on your end as well. Therefore I think this is just due to the change in the behavior of the annotation rather than a bug if my thought process is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants