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.

`clearInlineMocks` not working with `MockitoJUnit.rule()`

See original GitHub issue

Hi !

I recenlty updated to 2.25.0 to make use of clearInlineMocks but I end up with NotAMockException when I use it in combination with MockitoJUnit.rule().

Eg

class ExampleUnitTest {

    @get:Rule
    val mockitoRule: MockitoRule = MockitoJUnit.rule()

    @Mock
    lateinit var state: MainState

    @After
    fun clearMocks() {
        Mockito.framework().clearInlineMocks()
    }

    @Test
    fun randomTest() {
		state.something
		Mockito.verify(state).something
    }
}

ends up with

org.mockito.exceptions.misusing.NotAMockException: Argument passed to Mockito.mockingDetails() should be a mock, but is an instance of class com.melih.playground.ui.main.state.MainState!

	at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:49)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

and tests fail.

If I use @RunWith(MockitoJUnitRunner::class) tests pass, but I still get same exception at end of run.

If I remove rule & use MockitoAnnotations.initMocks(this) everything works fine.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ZacSweerscommented, Nov 18, 2020

Wow, currently experiencing this XKCD comic. Thank you @ttanxu, that resolved our issue.

Worth noting that RuleChain for ordering is discouraged in Junit 4.13+, so using Rule’s ordering works instead

// Use Mockito's new strict stubs listener, which fails early on mismatched types.
// This also handles validating mocks and closing them after the test to prevent leaks.
@Rule(order = 0)
@JvmField
val mockitoRule: MockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS)

// This needs to run _after_ mockitoRule is done
// https://github.com/mockito/mockito/issues/1902#issuecomment-727093806
@Rule(order = 1)
@JvmField
internal val clearInlineMocksRule: TestRule = ClearInlineMocksRule()
0reactions
ttanxucommented, Nov 14, 2020

I have a theory, but I didn’t verify that explicitly so I might be wrong. Just to put this theory here to guide folks to a possible solution.

JUnit runs @Before after each rule, and @After before each rule. In this particular case clearInlineMocks() removed internal handlers of all inline mocks, but MockitoJUnit rule still needs to use them which made this exception.

The solution is to wrap clearInlineMocks() in a TestRule as well, and use RuleChain to make sure the order of those 2 rules.

I’ll leave it to Mockito team to decide if it should offer more guidance around the use of clearInlineMocks(), or even encapsulate the use of it into MockitoJUnit rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

New API for clearing mock state in inline mocking (Since 2.25.0)
There is no clean way to mitigate this problem completely. ... @Rule public MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.
Read more >
mock-maker-inline makes test fail with "NotAMockException ...
Ok, I've found the issue: I was invoking. Mockito.framework().clearInlineMocks();. in a code invoked by @After annotation, but the correct ...
Read more >
MockitoFramework (Mockito 3.2.4 API) - javadoc.io
Mockito's "inline mocking" enables mocking final types, enums and final methods (read more in section 39 of Mockito javadoc).
Read more >
Superior Testing: Cleaning Up - Artur Dryomov
Nobody wants a product that breaks because there is not enough memory ... inline fun <reified T : Any> stub() = Mockito.mock(T::class.java, ...
Read more >
Getting Started with Mockito @Mock, @Spy, @Captor and ...
2.3. MockitoJUnit.rule() ... In this case, we must remember to make our rule public. 3. @Mock Annotation.
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