romeoopk
(Haseeb)
September 6, 2021, 3:46pm
1
HI
I would like to test my rest api in quarkus which internally calls elasticsearch using RestHighLevelClient.
My problem is that the RestHighLevelClient is not mockable and hence I cannot execute my unit test to test the api.
Any help is highly appreciated!
Regards,
dadoonet
(David Pilato)
September 7, 2021, 3:35am
2
Welcome!
Some users shared their solutions in this issue.
opened 04:02PM - 27 Mar 19 UTC
:Data Management/Java High Level REST Client
Team:Data Management
Hi,
I'm trying to unit test some code using RestHighLevelClient.
I have
…
```java
private synchronized void createIndex(String indexName) throws IOException {
if (!client.exists(new GetRequest(indexName), RequestOptions.DEFAULT)) {
CreateIndexRequest request = new CreateIndexRequest(indexName);
request.mapping(indexType, ElasticSearchConstants.MAPPING_SOURCE);
request.alias(new Alias(alias));
request.settings(ElasticSearchConstants.MAPPING_SETTINGS, XContentType.JSON);
client.indices().create(request, RequestOptions.DEFAULT);
}
}
```
Now i want to verify that client.indices().create was called.
RestHighLevelClient is not final and I was able to mock it.
Mock for RestHighLevelClient.indices() returns null by default.
So of course we want to mock that
```
when(elasticSearchClient.indices()).thenReturn(mock(IndicesClient.class));
```
BAM problem. IndicesClient is final class. I can't mock it.
So I can't test if create gets called and with which parameters.
IndicesClient should not be final class or indices() should return an interface and your final class implementation is hidden away.
This is pretty much basic stuff for public APIs guys.
Would that work for you?
system
(system)
Closed
October 5, 2021, 3:35am
3
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.