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
clearInlineMocks
not working with MockitoJUnit.rule()
#1902
Comments
clearInlineMocks
not working with MockitoJUnit
ruleclearInlineMocks
not working with MockitoJUnit.rule()
Also, it seems like you are using Kotlin. Could it be that the Rule doesn't work correctly when used in combination with Kotlin? |
From what's new for 2.25.0 & issue 1614 it sounds like something we should use to prevent
So |
Could you open a PR with a reproduction test case? Then I can take a look. |
Sorry for late response ! Code piece in question is from a fresh project, just added mockito & rule to |
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 The solution is to wrap I'll leave it to Mockito team to decide if it should offer more guidance around the use of |
Wow, currently experiencing this XKCD comic. Thank you @ttanxu, that resolved our issue. Worth noting that // 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() |
Summary: This change reverts commit d7afbc6 This also fixes the OOM caused by mockito memory leak: See: mockito/mockito#1614 The mockito internal map was using 95% of the memory at the time of the OOM. As suggested we are now calling ``` Mockito.framework().clearInlineMocks() ``` However we are calling only once per class instead of after each test method. This is because it interfears with JunitMockito rule. Alternative is to add rull ordering as suggested in: mockito/mockito#1902 (comment) Test Plan: This fixes the tests Reviewers: yshchetinin, hzare Reviewed By: yshchetinin, hzare Subscribers: yshchetinin, jenkins-bot, yugaware Differential Revision: https://phabricator.dev.yugabyte.com/D21333
Summary: This change reverts commit d7afbc6 This also fixes the OOM caused by mockito memory leak: See: mockito/mockito#1614 The mockito internal map was using 95% of the memory at the time of the OOM. As suggested we are now calling ``` Mockito.framework().clearInlineMocks() ``` However we are calling only once per class instead of after each test method. This is because it interfears with JunitMockito rule. Alternative is to add rull ordering as suggested in: mockito/mockito#1902 (comment) Test Plan: This fixes the tests Reviewers: yshchetinin, hzare Reviewed By: yshchetinin, hzare Subscribers: yshchetinin, jenkins-bot, yugaware Differential Revision: https://phabricator.dev.yugabyte.com/D21333
Hi !
I recenlty updated to
2.25.0
to make use ofclearInlineMocks
but I end up withNotAMockException
when I use it in combination withMockitoJUnit.rule()
.Eg
ends up with
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.The text was updated successfully, but these errors were encountered: