我正在为 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/