I need to mock RestHighLevelClient to test my code. Basically when i call "search" method of RestHighLevelClient i get either UnfinishedStubbingException or WrongTypeOfReturnValue.
PowerMockito.doReturn(searchResponse).when(client.search(searchRequest, RequestOptions.DEFAULT));
this example throws UnfinishedStubbingException.
PowerMockito.when(client.search(searchRequest, RequestOptions.DEFAULT)).thenReturn(searchResponse);
this example throws WrongTypeOfReturnValue.
Here is my config
@RunWith(PowerMockRunner.class)
@PrepareForTest(value = {
RestHighLevelClient.class
...otherClasses
})
I googled about both exceptions but, i am not calling "mock.someMethod()" inside "thenReturn" method. Seems like every approach is not working.
It's an open issue in elasticsearch repo. All I could do is the following:
@Mock
private RestHighLevelClient restHighLevelClient;
@Before
public void setup() {
restHighLevelClient = mock(RestHighLevelClient.class);
}
and then use it as follow
when(elasticClientHandler.createRestClient())
.thenReturn(restHighLevelClient);