# Mocking RestHighLevelClient

**URL:** <https://discuss.elastic.co/t/mocking-resthighlevelclient/201211>\
**Category:** Elasticsearch\
**Created:** [September 26, 2019, 10:55am UTC](https://discuss.elastic.co/t/mocking-resthighlevelclient/201211 "2019-09-26T10:55:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DevShan](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@DevShan](https://discuss.elastic.co/u/DevShan)\
**Post date:** [September 26, 2019, 10:55am UTC](https://discuss.elastic.co/t/mocking-resthighlevelclient/201211/1 "2019-09-26T10:55:23Z")

</div>

I'm trying to use groovy to write Junit, for a class that uses RestHighLevelClient to call ES and get the search results.  
I am not able to mock the client using GroovyMock or normal mock.  
Please let me know how to approach this Junit use case

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 26, 2019, 1:56pm UTC](https://discuss.elastic.co/t/mocking-resthighlevelclient/201211/2 "2019-09-26T13:56:34Z")

</div>

Can you please be more specific, what the problem is, which exceptions are thrown, etc?

---

<div class="post-metadata">

**Author:** ![DevShan](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@DevShan](https://discuss.elastic.co/u/DevShan)\
**Post date:** [September 26, 2019, 4:57pm UTC](https://discuss.elastic.co/t/mocking-resthighlevelclient/201211/3 "2019-09-26T16:57:51Z")

</div>

Usually when we mock an object of a specific class, the control will not go to the mocked class while unit testing and we can make a specific method from that class , return what result we want when the test is run . But that isn't the case when I mock RestHighLevelClient . Please see the code snippet below , Imagine I'm trying to write a unit test case for the following java class using groovy:

Public class ESService {

Public RestHighLevelClient restHighLevlClient;//(intialisation logic, where we define the url etc)

Public getsearchResult(parameters){  
//random code  
SearchResponse response = restHighLevlClient.search(searchRequest);  
}  
If I mock RestHighLevelClient , the control shouldn't go inside search, but that isn't the case , it goes into RestHighLevelClient class and tries to intialize the restClient inside it which throws null pointer exception because I haven't given any URL or anything.

---

<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:** [October 24, 2019, 4:57pm UTC](https://discuss.elastic.co/t/mocking-resthighlevelclient/201211/4 "2019-10-24T16:57:57Z")

</div>

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