# How to test a service which uses RestHighLevelClient?

**URL:** <https://discuss.elastic.co/t/how-to-test-a-service-which-uses-resthighlevelclient/151674>\
**Category:** Elasticsearch\
**Created:** [October 9, 2018, 3:34pm UTC](https://discuss.elastic.co/t/how-to-test-a-service-which-uses-resthighlevelclient/151674 "2018-10-09T15:34:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inna\_Korobikhina](https://avatars.discourse-cdn.com/v4/letter/i/8e8cbc/32.png) [@Inna\_Korobikhina](https://discuss.elastic.co/u/Inna_Korobikhina)\
**Post date:** [October 9, 2018, 3:34pm UTC](https://discuss.elastic.co/t/how-to-test-a-service-which-uses-resthighlevelclient/151674/1 "2018-10-09T15:34:23Z")

</div>

Hi, I have a service which uses RestHighLevelClient and which I want to unit test. I have a method that has the following lines:

```
 ....
    IndexRequest request = new IndexRequest(index, type).source(getContentBuilder(update));
    IndexResponse response = restHighLevelClient.index(request);
    return response.getResult() == CREATED;
....

```

So, in my test I'm trying to do the following:

```
@Mock
RestHighLevelClient esClient = Mockito.mock(RestHighLevelClient.class);
@InjectMocks
MyService updateService;

```

And directly in the test:

```
when(esClient.index(any(IndexRequest.class)))
          .thenReturn(new IndexResponse());

```

But I get the NullPointerExeption in this line.

I found information that we cannot mock RestHighLevelClient. I would like to get more details about it. So, is it possible to test my method using something like that?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 6, 2018, 3:34pm UTC](https://discuss.elastic.co/t/how-to-test-a-service-which-uses-resthighlevelclient/151674/2 "2018-11-06T15:34:24Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
