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

Mocking a bean in a Spy does not always work #12

Closed
antoinemeyer opened this issue Jan 15, 2022 · 4 comments · Fixed by #24 · May be fixed by #15
Closed

Mocking a bean in a Spy does not always work #12

antoinemeyer opened this issue Jan 15, 2022 · 4 comments · Fixed by #24 · May be fixed by #15

Comments

@antoinemeyer
Copy link
Owner

antoinemeyer commented Jan 15, 2022

Sometimes works and sometimes does not because of the non-deterministic aspect of MockInBean / SpyInbean definition parsing.

  • Works when the mock is created before the spy (because the spy will use the fields already injected into the bean).
  • Does not work when the spy is created before the mock (because the mock will be injected inside the spring bean and not the spy).

This is because the Spy does not delegate to the original object but creates a copy (as documented in https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Spy.html).

Could be fixed by also injecting mocks into spies.

@antoinemeyer antoinemeyer linked a pull request Jan 15, 2022 that will close this issue
@antoinemeyer
Copy link
Owner Author

Will be fixed in #11 by dbb5bb8

@fuesec
Copy link

fuesec commented Mar 18, 2024

has this been fixed?

@antoinemeyer
Copy link
Owner Author

Not yet in master. It's quite an edge case but will push a fix for it in the coming weeks.

@antoinemeyer
Copy link
Owner Author

Fixed in version boot2-v1.6.

Let me know if you still have issue

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