question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RedundantListenerException when updating to mockito 3.2.4 with parallel test execution

See original GitHub issue

Hi!

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:open
  • Created 4 years ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
mockitoguycommented, Jan 14, 2020

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?

  • try to fix MockitoJUnitRunner. This is not something easy to reproduce let alone fix. More over, JUnit Runner is old - now we use JUnit5 (JUnit Jupiter). Mockito team won’t prioritize work on JUnit4 Runners. Take a stab at it if you feel strong! (I opened a PR that makes your repro repo a bit easier to work with: https://github.com/julius-d/mockito-upgrade-problem/pull/1).
  • workarounds:
    • use JUnit Rule instead of the Runner: public @Rule MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);. Rules are simpler, the JUnit Rules API is much cleaner.
    • use JUnit5.

Thoughts?

0reactions
Gr33nbl00dcommented, Mar 4, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found