java - Mockito 模拟调用点指向 Junit 代码(ParentRunner)

标签 java unit-testing junit mocking mockito

以前从未见过此问题 - 当使用 verifyverifyInteractions 且测试失败时,Mockito 的调用列表指向 JUnit 代码而不是应用程序代码:

[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).

我正在使用 Mockito 1.10.19 和 JUnit 4.12。代码非常标准:

public class MyTest {
    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    private Counter mockCounter;

...

    @Test
    public void test() {
       ...
       verifyNoMoreInteractions(mockCounter);
    }

首先,我还使用了 ExpectedException 规则:

@Rule
public ExpectedException thrown = ExpectedException.none();

...这导致了额外的问题,因为它捕获了 Mockito 的验证失败作为意外异常:

[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).

我正在开发一个新的代码库,以前从未见过这个问题。似乎 Mockito 或 JUnit 清理堆栈跟踪过于积极或其他什么......

我可以在没有ExpectedException的情况下生活,但是在未来测试失败的情况下没有模拟调用点似乎很烦人。我怎样才能解决这个问题?我saw withSettings().verboseLogging() 的概念,但我不想始终记录调用,只有在测试失败时才记录。

最佳答案

看来 MockitoRule 是罪魁祸首。在 setUp() 方法中用 MockitoAnnotations.initMocks(this); 替换它可以完全解决问题,即使使用 ExpectedException 规则也是如此。 p>

已经fixed在 Mockito 2.x 中,但未向后移植到 1.x

关于java - Mockito 模拟调用点指向 Junit 代码(ParentRunner),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59059398/

相关文章:

JavaFX 将 VBox 置于 GridPane 内居中

c# - 依赖注入(inject)解析和单元测试

java - 如何使用相同的库两次配置不同版本的 Maven

java - 模拟库调用

java - 如何在 Android Marshmallow 上请求权限以进行 JUnit 测试

java - 使用java打开新选项卡

Java Play框架+H2数据库,并发操作性能

java - 从 URL 获取标签之间的所有文本内容?

基于参数值的 C++ 假/模拟返回值

java - 尝试在 junit 中模拟 X500Principal 时出现 LinkageError