1

After migration of out legacy project from Ant to Gradle from time to time we started to experience following error:

 org.mockito.exceptions.misusing.UnfinishedMockingSessionException: 
    Unfinished mocking session detected.
    Previous MockitoSession was not concluded with 'finishMocking()'.
    For examples of correct usage see javadoc for MockitoSession class.
        at org.mockito.junit.jupiter.MockitoExtension.beforeEach(MockitoExtension.java:153)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachCallbacks$1(TestMethodTestDescriptor.java:161)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$5(TestMethodTestDescriptor.java:197)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:197)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachCallbacks(TestMethodTestDescriptor.java:160)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
        at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:171)
        at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.executeNonConcurrentTasks(ForkJoinPoolHierarchicalTestExecutorService.java:141)
        at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.invokeAll(ForkJoinPoolHierarchicalTestExecutorService.java:121)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
        at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:171)
        at java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:189)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinTask.doJoin(ForkJoinTask.java:389)
        at java.util.concurrent.ForkJoinTask.join(ForkJoinTask.java:719)
        at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.joinConcurrentTasksInReverseOrderToEnableWorkStealing(ForkJoinPoolHierarchicalTestExecutorService.java:148)
        at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.invokeAll(ForkJoinPoolHierarchicalTestExecutorService.java:122)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
        at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:171)
        at java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:189)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:175)

It happens with probability 50% on pipeline and it is not reproduced locally. I have no idea why. Could you please give me some ideas where to take a look ?

Update 1:

I've updated Mockito version from 3.5.10 to 3.11.2 but it didn't help

Update 2:

I've tried to mark all my tests classes with:

@MockitoSettings(strictness = Strictness.LENIENT)

but unfortunately it didn't help.

P.S.

As I understand Mockito creates some entity called MockitoSession under the hood but there are some bug in the library and this session is not closed after test execution or maybe next test starts too early. The most obvious solution for me is getting access to that session and close it each time after test is finished (in method marked @AfterClass for example) but I can't find way how to acces that session

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • I remember a few things like this we had.. are you using the latest version of mockito? – Eugene Jul 29 '21 at 15:55
  • 1
    @Eugene I've just updated version from 3.5.10 to 3.11.2 and will report if it help. For now I don't have finished pipelines – gstackoverflow Jul 29 '21 at 16:00
  • @Eugene I started pipeline 10 times and it failed once with the same error unfortunately. – gstackoverflow Jul 29 '21 at 16:32
  • may be use the same `@Listeners` [as suggest here?](https://github.com/mockito/mockito-testng/issues/40) – Eugene Jul 29 '21 at 16:40
  • Is it correct that I should mark each test class with @Listeners(MockitoTestNGListener.class) ? BTW I don't have both: Listeners and MockitoTestNGListener in my classpath – gstackoverflow Jul 29 '21 at 16:49
  • As you're using Junit 5 I don't see how TestNG listeners would help? – tgdavies Jul 30 '21 at 11:04
  • @tgdavies I don't see either. I use JUnit5 but I have junit4 in classpath and in some tests it could be used I am afraid. Our code bas is huge. Do you think it makes sense to try to remove junit4 from dependencies ? – gstackoverflow Jul 30 '21 at 11:16
  • Perhaps this is being triggered by some flaky behaviour in the test. I'd look at the differences in the logs between a successful and failed run, and look at the test to see what might fail occasionally (e.g. talking to an external system, something taking a different amount of time) and induce that failure locally. – tgdavies Jul 30 '21 at 23:03
  • Worth adding the exact versions of junit and mockito (and junit 4 I guess). Also the code of the actual testy help. – tgdavies Jul 30 '21 at 23:08
  • It would be very helpful if you could provide both the test that produces this error and the build configuration over which the test is run. In general, I think first of all that the problem is more in the test implementation and execution and not that the Mockito library contains a bug. You can see in the stack trace that Junit is used as the basic test framework, so using TestNG Listener makes no sense. – Ingo Griebsch Aug 02 '21 at 08:06
  • Admins are not smart guys and for some reason decided to close the question but I still don't have an answer – gstackoverflow Aug 10 '21 at 11:53
  • As I and @IngoGriebsch have asked, please edit your question and include the code of your test. Have you compared the logs between successful and unsuccessful runs? – tgdavies Aug 11 '21 at 00:20

0 Answers0