Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnfinishedMockingSessionException with inner test classes #1578

Closed
mouyang opened this issue Jan 3, 2019 · 0 comments
Closed

UnfinishedMockingSessionException with inner test classes #1578

mouyang opened this issue Jan 3, 2019 · 0 comments

Comments

@mouyang
Copy link
Contributor

mouyang commented Jan 3, 2019

This appears to be a regression from 1.10.19 and 2.x-beta since it was reported as fixed in #353, but I was unable to get the sample test class to work in the release/2.x branch (also reported to fail in 2.13.0, 2.1.0-beta.125, and master too). The root cause is JUnitRule attempting to add a UniversalTestListener a second time to a ThreadSafeMockingProcess.

Sample Test class

@RunWith(HierarchicalContextRunner.class)
public class HierarchicalMockitoTest {
    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    private Runnable runnable;

    public class Context {
        @Test
        public void test() throws Exception {
            runnable.run();
        }
    }
}

Exception Thrown

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.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:42)
	at org.mockito.internal.junit.JUnitRule$1.evaluateSafely(JUnitRule.java:52)
	at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:43)
	at de.bechte.junit.runners.context.statements.StatementExecutor.execute(StatementExecutor.java:28)
	at de.bechte.junit.runners.context.processing.MethodExecutor.run(MethodExecutor.java:83)
	at de.bechte.junit.runners.context.processing.MethodExecutor.run(MethodExecutor.java:57)
	at de.bechte.junit.runners.context.statements.RunChildren.evaluate(RunChildren.java:38)
	at de.bechte.junit.runners.context.statements.RunAll.evaluate(RunAll.java:27)
	at de.bechte.junit.runners.context.statements.StatementExecutor.execute(StatementExecutor.java:28)
	at de.bechte.junit.runners.context.HierarchicalContextRunner.run(HierarchicalContextRunner.java:134)
	at de.bechte.junit.runners.context.processing.ContextExecutor.run(ContextExecutor.java:26)
	at de.bechte.junit.runners.context.processing.ContextExecutor.run(ContextExecutor.java:15)
	at de.bechte.junit.runners.context.statements.RunChildren.evaluate(RunChildren.java:38)
	at de.bechte.junit.runners.context.statements.RunAll.evaluate(RunAll.java:27)
	at de.bechte.junit.runners.context.statements.StatementExecutor.execute(StatementExecutor.java:28)
	at de.bechte.junit.runners.context.HierarchicalContextRunner.run(HierarchicalContextRunner.java:134)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

I have a fix in progress which involves lazy initialization (
https://github.com/mouyang/mockito/commit/540ad11d5a0d4066a73b3e46a932733d7ed816df). I haven’t submitted a PR yet because I’m waiting for the library I used to reproduce the issue to make a new release with a required fix bechte/junit-hierarchicalcontextrunner#32. Also I need to clean up the commit to use reference this issue instead of 353. I suppose I can move forward with a PR if there was a way to reproduce the issue without using that library but I haven’t thought of a way to do that.

Thanks to @UrsMetz for verifying the regression and providing a sample test class.

SamBarker pushed a commit to SamBarker/mockito that referenced this issue Feb 25, 2019
…ckito#1596)

- Problem - JUnitRule detects an unfinished mocking session when the apply method is invoked more than once.  This commonly happens with Runners that enable developers to write nested test classes.
- Fix - If a MockitoSession has been established for JUnitRule, then use that and initialize any new mocks (typically from a nested test class).
- Test - Instantiate one @rule and assign it to a second one.  This will simulate the apply method being invoked more than once without introducing a new test dependency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant