Mocking is sometimes not applied when running a multi-class testsuite

36 views
Skip to first unread message

Anton Oellerer

unread,
Jan 20, 2023, 10:53:41 AM1/20/23
to mockito
Hey,
I am testing a service which heavily relies on project reactor.
For many tests I am mocking the return value of the component responsible for API calls. The tests are split over multiple files.
When I run the tests of one file, they are green, but when I execute all of the test files at once, some tests fail, with the error message indicating that the mocking did not succeed (Either the injected component returned `null`, or the implementation of the actual component is invoked).
In the logs, there is no information about the mocking failing.
I attached a small code example.
Do you know what the reason is/might be for the tests failing?
For me the issue seems like a race condition, but I don't know where and how this might happen, and how I can check and fix this.
Best regards
Anton Oellerer
mockitoExample.java

Anton Oellerer

unread,
Mar 30, 2023, 8:58:44 AM3/30/23
to mockito

I fixed this by annotating each class with `@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)`

Reply all
Reply to author
Forward
0 new messages