Hello, i need some help to working with unit test.
I want to create unit test to update data using UpdateByQueryRequestBuilder
But i always got an error like these
java.lang.NullPointerException
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:404)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:53)
at com.dondon.UpdateServiceImpl.lambda$updateStatus $0(UpdateServiceImpl.java:108)
this is my update function
@Override
public Boolean updateStatus() {
if(elasticSearchService.isIndexExists(ElasticSearchIndexConstant.ROUTE_MANAGEMENT_MAP_INDEX)){
UpdateByQueryRequestBuilder updateByQueryRequestBuilder = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
updateByQueryRequestBuilder.source(ElasticSearchIndexConstant.ROUTE_MANAGEMENT_MAP_INDEX);
updateByQueryRequestBuilder
.script(
new Script(
ScriptType.INLINE, "painless, "ctx._source.status".concat("='")
.concat("ACTIVE")
.concat("'"),
Collections.emptyMap()
)
)
.source()
.setQuery(
QueryBuilders.matchQuery("FIELD", "SOME_UNIQUE_CODE")
);
// this line always failed
// i already stub get() method
BulkByScrollResponse response = updateByQueryRequestBuilder.get();
return true;
}
return false;
}
and this my unit test that not working
@Mock
private UpdateByQueryRequestBuilder updateByQueryRequestBuilder;
@Mock
private SearchRequestBuilder searchRequest;
@Mock
private BulkByScrollResponse bulkByScrollResponse;
@Mock
private SearchResponse searchResponse;
@Test
public void updateStatusRouteManagementBasedOnAirlineConfigurationActiveTest() throws Exception {
when(elasticSearchService.isIndexExists(ElasticSearchIndexConstant.ROUTE_MANAGEMENT_MAP_INDEX)).thenReturn(true);
when(updateByQueryRequestBuilder.source(ElasticSearchIndexConstant.ROUTE_MANAGEMENT_MAP_INDEX)).thenReturn(updateByQueryRequestBuilder);
when(updateByQueryRequestBuilder.script(new Script(ScriptType.INLINE, "painless", "ctx._source.status='ACTIVE'",
Collections.emptyMap()))).thenReturn(updateByQueryRequestBuilder);
when(updateByQueryRequestBuilder.source()).thenReturn(searchRequest);
when(searchRequest.setQuery(any())).thenReturn(searchRequest);
when(updateByQueryRequestBuilder.get()).thenReturn(bulkByScrollResponse);
when(updateByQueryRequestBuilder
.source(ElasticSearchIndexConstant.ROUTE_MANAGEMENT_MAP_INDEX)
.script(new Script(ScriptType.INLINE, "painless", "ctx._source.status='ACTIVE'",
Collections.emptyMap()))
.source().setQuery(any())
.get()).thenReturn(searchResponse);
Boolean updated = updateService.updateStatus();
assertEquals(true, updated);
}