Never saw this problem before - when using verify
or verifyInteractions
and the test fails, Mockito's list of onvocations points to JUnit code and not application code:
[junit] No interactions wanted here:
[junit] -> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
[junit] But found this interaction on mock 'mockCounter':
[junit] -> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
[junit] ***
[junit] For your reference, here is the list of all invocations ([?] - means unverified).
[junit] 1. [?]-> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
[junit] 2. [?]-> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
[junit] 3. -> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
[junit]
[junit] junit.framework.AssertionFailedError:
[junit] No interactions wanted here:
[junit] But found this interaction on mock 'mockCounter':
[junit] ***
[junit] For your reference, here is the list of all invocations ([?] - means unverified).
I am using Mockito 1.10.19
and JUnit 4.12
. The code is pretty standard:
public class MyTest {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
private Counter mockCounter;
...
@Test
public void test() {
...
verifyNoMoreInteractions(mockCounter);
}
First I was also using ExpectedException
rule:
@Rule
public ExpectedException thrown = ExpectedException.none();
...which caused additional problems, because it caught Mockito's verification failure as an unexpected exception:
[junit] No interactions wanted here:
[junit] -> at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
[junit] But found this interaction on mock 'mockCounter':
[junit] -> at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
[junit] ***
[junit] For your reference, here is the list of all invocations ([?] - means unverified).
[junit] 1. [?]-> at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
[junit] 2. [?]-> at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
[junit] 3. -> at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
[junit]
[junit] junit.framework.AssertionFailedError:
[junit] No interactions wanted here:
[junit] But found this interaction on mock 'mockCounter':
[junit] ***
[junit] For your reference, here is the list of all invocations ([?] - means unverified).
I am working on a new code base and never seen this problem before. It seems like either Mockito or JUnit clean up stack traces too aggressively or something...
I can live without ExpectedException
but it seems pretty annoying to not have mock invocation points in case of future test failures. How can I fix this? I saw the notion of withSettings().verboseLogging()
but I don't want to log invocations at all times, only if the test fails.
It seems that MockitoRule
is the culprit. Replacing it with MockitoAnnotations.initMocks(this);
in setUp()
method fixes the problem completely, even when ExpectedException
rule is used.
It has been fixed in Mockito 2.x
but not back-ported to 1.x
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.