`clearInlineMocks` not working with `MockitoJUnit.rule()`
See original GitHub issueHi !
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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >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
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 insteadI 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 caseclearInlineMocks()
removed internal handlers of all inline mocks, butMockitoJUnit
rule still needs to use them which made this exception.The solution is to wrap
clearInlineMocks()
in aTestRule
as well, and useRuleChain
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 intoMockitoJUnit
rule.