尝试在Mockito中模拟ElasticClient时出现NPE错误 [英] Getting NPE error when trying to mock ElasticClient in Mockito

查看:158
本文介绍了尝试在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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆