java - Mockito 无法模拟此类 JestClient

标签 java unit-testing mockito elasticsearch-jest

我正在为 JestClient 包装器编写单元测试。我尝试使用 Mockito 来模拟 JestClient,但出现以下错误:

Mockito cannot mock this class: interface io.searchbox.client.JestClient

Mockito can only mock visible & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

这是我定义模拟的方式:

@Bean(name = Identifiers.JEST_CLIENT)
@Profile(Identifiers.PROFILE_UNIT_TEST)
public JestClient getJestClient() {
    JestClient client = Mockito.mock(JestClient.class);
    return client;
}

我不确定为什么会收到错误,因为 JestClient 只是一个接口(interface)。我正在使用:

开玩笑:0.1.6

Mockito:2.0.36-beta

Spring :4.2.4

编辑:我认为问题出在 Elasticsearch 库上。我使用的是 Elasticsearch 1.5.2;当我切换到2.2.0时,问题就消失了。不幸的是,这对我没有帮助,因为我正在谈论的集群在 1.5.2 上运行。

最佳答案

就我而言,问题是我还使用 Elasticsearch 1.5.2 作为我的库之一。 2.0 之前的 Mockito 和 Elasticsearch 版本存在一个错误,Mockito 无法工作(请参阅 https://github.com/mockito/mockito/issues/341 )。

关于java - Mockito 无法模拟此类 JestClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38417642/

相关文章:

scala - 如何在 Scala 测试中模拟 Dispatch http 客户端?

java - 模拟未被注入(inject)

java - 使用 void 方法的 doNothing 方法时出现 UnfinishedStubbingException

java - 从命令行使用 evosuite

java - RESTful API - 批量操作的分块响应

java - 在不使用时钟的情况下用 Java 实现 "task based"程序

java - 为什么我会看到 "the temporary folder has not yet been created"消息?

c# - 单元测试组合根?

javascript - 如何创建 Karma/Jasmine 单元测试来检查依赖于其他模块的 Angular 服务

java - 如何在 Gradle 中为单个测试类并行执行 JUnit 测试