Hi, I am trying to write a unit test with JUnit & Mockito for client.update() method but I am stuck.
Here is my updateMyId() method to test:
var searchRequest = Factory.createDocumentByIdSearchRequest(indexName, id, aParamater);
var searchResponse = elasticsearchJavaApiClient.search(searchRequest, MyCustom.class);
var singleDocumentHit = getSingleRawDocumentHit(searchResponse, id);
var updateRequest = Factory.createUpdateRequest(firstParam, secondParam, singleDocumentHit);
elasticsearchJavaApiClient.update(updateRequest, MyCustom.class);
and i tried this:
SearchResponse<MyCustom> mockedSearchResponse = SearchResponseDataProvider.createSearchResponseForASingleDocument();
when(elasticsearchJavaApiClient.search(any(SearchRequest.class), eq(MyCustom.class))).thenReturn(mockedSearchResponse);
var expectedUpdateResponse = UpdateResponse.of(update -> update
.index("my_index")
.id(id)
.primaryTerm(1L)
.seqNo(1L)
.shards(ShardStatistics.of(sh -> sh
.successful(4)
.failed(0)
.total(4))
).version(1L)
.result(Result.Updated));
var updateRequest = Factory.createUpdateRequest(firstParam, secondParam, mockedSearchResponse.hits().hits().get(0));
when(elasticsearchJavaApiClient.update(eq(updateRequest), eq(MyCustom.class))).thenReturn(expectedUpdateResponse);
elasticSearchServiceImpl.updateMyId(paramA, id, paramB, paramC, paramD);
verify(elasticsearchJavaApiClient).update(eq(updateRequest), eq(MyCustom.class));
I mean I tried literally every combination but still couldn't have test past. I appreciate any help. Thanks.