Using @TestExecutionListeners(MockitoTestExecutionListener.class)
Two separate test classes should be independent, even when reusing the same cached app context. Stubbing inside a test class should not remain in the other. Same for invocation counts.
Comment From: wilkinsona
What are you using MockitoTestExecutionListener
for? As mentioned in its javadoc its intended for use with @MockBean
. @MockBean
will automatically reset the mock after each test.
Comment From: cdalexndr
I'm using it for @SpyBean
Comment From: wilkinsona
@SpyBean
should have the same automatic reset behaviour, tunable via its reset
attribute. If your @SpyBean
spies aren't being reset, then something's not behaving as expected. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: cdalexndr
Test sample repo: https://github.com/cdalexndr/spring-boot-issue-25366
Comment From: wilkinsona
Thanks. The default behaviour of @TestExecutionListeners
is to replace the default execution listeners which will mean that you're losing ResetMocksTestExecutionListener
. I think it'll work if you either specify ResetMocksTestExecutionListener
or configure the merge mode to be MERGE_WITH_DEFAULTS
.
Comment From: cdalexndr
Indeed, using @TestExecutionListeners({MockitoTestExecutionListener.class, ResetMocksTestExecutionListener.class})
tests succeed.
But shouldn't ResetMocksTestExecutionListener
be part of MockitoTestExecutionListener
? Why are they separate?
This separation allows invalid behavior, because @Spybean
is enabled by MockitoTestExecutionListener
, but only partially, because it's reset
field won't work unless using ResetMocksTestExecutionListener
.
So it seems natural to bundle them together, and using only one instance lowers occurrences of bugs due to missing test listener.
Note that with testng
I'm required to use @TestExecutionLisetners
annotation. I guess on JUnit, the listeners are automatically added.
Comment From: wilkinsona
They were written a long time ago so I can't remember all of the details, but one reason for them being separate is their different orders. I don't think it would be possible to combine them without changing their behaviour and breaking something. If you want to use TestNG, I'm afraid you'll have to remember to specify both listeners.
Comment From: cdalexndr
docs should be updated to include info about ResetMocksTestExecutionListener
Comment From: wilkinsona
If you'd like to propose something, we'd be more than happy to review a pull request.