RedundantListenerException when updating to mockito 3.2.4 with parallel test execution
See original GitHub issueHi!
I am trying to update a codebase to mockito 3.2.4!
After the update some tests fail with
org.mockito.exceptions.misusing.RedundantListenerException:
Problems adding Mockito listener.
Listener of type 'MismatchReportingTestListener' has already been added and not removed.
It indicates that previous listener was not removed according to the API.
When you add a listener, don't forget to remove the listener afterwards:
Mockito.framework().removeListener(myListener);
For more information, see the javadoc for RedundantListenerException class.
I was able to find out that the tests do not fail if I change the configuration of the maven-surefire-plugin
to not run test in parallel.
The codebase uses maven-surefire-plugin 2.22.1 maven-compiler-plugin 3.8.0 junit 4.12 java 1.8
I created an example project which reproduces the problem
https://github.com/julius-d/mockito-upgrade-problem
clone it and run mvn test
It will fail with RedundantListenerException
The tests do nothing special, they only waste time:
@Test
public void name() {
IntStream.range(0, Integer.MAX_VALUE).boxed()
.forEach(i -> assertEquals(i, i));
}
- The mockito message in the stacktrace have useful information, but it didn’t help
- The problematic code (if that’s possible) is copied here; Note that some configuration are impossible to mock via Mockito
- Provide versions (mockito / jdk / os / any other relevant information)
- Provide a Short, Self Contained, Correct (Compilable), Example of the issue (same as any question on stackoverflow.com)
- Read the contributing guide
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:9 (4 by maintainers)
Top Results From Across the Web
RedundantListenerException (Mockito 3.2.4 API) - javadoc.io
Reported when instance of MockitoListener is being added to Mockito (see MockitoFramework ) and there is already a listener with this implementation type ......
Read more >mockito - Bountysource
I am trying to update a codebase to mockito 3.2.4! After the update some tests fail with org.mockito.exceptions.misusing.RedundantListenerException: Problems ...
Read more >Maven Surefire Plugin – Fork Options and Parallel Test ...
The surefire offers a variety of options to execute tests in parallel, allowing you to make best use of the hardware at your...
Read more >Running JUnit Tests in Parallel with Maven - Baeldung
Next, to demonstrate parallel test execution, we'll use a test suite with two test classes each having a few methods. In fact, any...
Read more >when I run mockito test occurs WrongTypeOfReturnValue ...
I have also confronted the error in a multi-threaded test. ... Spring boot ships with its own Mockito, which is now also upgraded...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I dug into this issue here’s what happens. When Maven Surefire plugin executes the tests with parallel “suitesAndClasses” feature I see an interesting symptom:
We expect the same thread to “start” the test and “finish” it. Internally, Mockito uses “ThreadLocal” to protect global state (global state is required to accommodate nice & sweet Mocking API).
What’s next?
public @Rule MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
. Rules are simpler, the JUnit Rules API is much cleaner.Thoughts?
I found out something very interesting: I had some classes using MockitoRunner others were not using it (No Runner Defined). The problem is gone when i let the other classes also use the MockitoRunner