尝试在Mockito中模拟ElasticClient时出现NPE错误

编程入门 行业动态 更新时间:2024-10-23 21:39:49
本文介绍了尝试在Mockito中模拟ElasticClient时出现NPE错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是Mockito框架的新手,我正在为包含ElasticClient的类编写测试.下面是我的实际方法:

I am new to Mockito framework, and I am working on writing tests for the class containing ElasticClient. Below is my actual method:

@Service @Slf4j public class CreateIndex { private final RestHighLevelClient elasticClient; @Autowired public IndexService(final RestHighLevelClient elasticClient) throws IOException { this.elasticClient = elasticClient; } public boolean createIndex(String id, String index) { try { IndexRequest request = new IndexRequest(index); request.id(id); elasticClient.index(request, RequestOptions.DEFAULT); return true; } catch (IOException e) { log.warn(e.getMessage()); } return false; }

我的测试代码如下:

public class TestCreateIndex { CreateIndex createIndex; @Mock RestHighLevelClient elasticClient; @Rule public MockitoRule rule = MockitoJUnit.rule(); @Before public void before() throws IOException { createIndex = new CreateIndex(elasticClient); } @Test public void TestCreateIndex() throws IOException { IndexRequest request = new IndexRequest("1"); request.id("1"); Mockito.when(elasticClient.index(request,(RequestOptions.DEFAULT))).thenReturn(indexResponse); } }

对于行 Mockito.when(elasticClient.index(request,RequestOptions.DEFAULT)).thenReturn(indexResponse); (RequestOptions是某些类),我得到以下错误:

For the line Mockito.when(elasticClient.index(request,RequestOptions.DEFAULT )).thenReturn(indexResponse);(RequestOptions is some Class), I am getting below error:

java.lang.NullPointerException: Cannot invoke "org.elasticsearch.client.RestClient.performRequest(org.elasticsearch.client.Request)" because "this.client" is null at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1514) at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1484) at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1454) at org.elasticsearch.client.RestHighLevelClient.index(RestHighLevelClient.java:871)

不确定如何正确模拟elasticClient.请帮忙.

Not sure, how to mock elasticClient properly. Please help.

推荐答案

问题是您尝试存根的方法是最终的:

The problem is that the method you are trying to stub is final:

public final IndexResponse index(IndexRequest indexRequest, RequestOptions options) throws IOException

Mockito不支持开箱即用地模拟最终方法,相反,它调用了导致NPE的real方法,因为未初始化 private final RestClient客户; .

Mockito does not support mocking final methods out of the box, instead it calls the real method, which causes NPE, since private final RestClient client; is not initialized.

幸运的是,对最终方法进行存根可以很容易地添加为配置选项.请参见使用Mockito模拟最终类和方法

Fortunately, stubbing final methods can be easily added as a configuration option. See Mock Final Classes and Methods with Mockito

在将Mockito用于模拟最终类和方法之前,需要对其进行配置.

Before Mockito can be used for mocking final classes and methods, it needs to be configured.

我们需要向项目的src/test/resources/mockito-extensions目录中添加一个名为org.mockito.plugins.MockMaker的文本文件,并添加一行文本:

We need to add a text file to the project's src/test/resources/mockito-extensions directory named org.mockito.plugins.MockMaker and add a single line of text:

mock-maker-inline

在加载扩展名时,Mockito会检查扩展目录中的配置文件.该文件可模拟最终方法和类.

Mockito checks the extensions directory for configuration files when it is loaded. This file enables the mocking of final methods and classes.

或者,从Mockito 2.7.6开始,您可以使用 mockito-inline 工件(而不是 mockito-core )来实现内联模拟制作

Alternatively, since mockito 2.7.6, you can use mockito-inline artifact (instead of mockito-core) that enables inline mock making

更多推荐

尝试在Mockito中模拟ElasticClient时出现NPE错误

本文发布于:2023-10-31 06:00:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Mockito   ElasticClient   NPE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!