UpdateByQueryRequestBuilder unit test

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);
}

this is hard to tell without knowing what your client variable looks like. I am not sure what you are exactly trying to achieve with the mocking here and what exactly is tested though.

I want to create some functional test to make sure the business logic is passed.
The business logic is :
check index is exist or not, if index is exist then update the existing data

this is the client

@Mock
private TransportClient client;

Hope it's clear enough

I dont know enough about your mocking library, but I guess you need to mock the execute method which is called by the get(). The easiest way to understand the flow is probably to try with a non-mocked version first and just follow it through with a debugger to understand which methods needs to be mocked.

You could also take a look at the x-pack tests as they are using mockito to mock out some client calls (note, those tests are not under an open source license).

I am using Mockito Library and Spring

i've test several possibility but none of them are working.
here is my last update

BulkByScrollResponse bulkByScrollResponse = mock(BulkByScrollResponse.class);
    when(bulkByScrollResponse.getBulkFailures()).thenReturn(new ArrayList<>());

    ListenableActionFuture<BulkByScrollResponse> listenableActionFuture = mock(ListenableActionFuture.class);
    when(listenableActionFuture.actionGet()).thenReturn(bulkByScrollResponse);

    UpdateRequestBuilder updateRequestBuilder = mock(UpdateRequestBuilder.class);
    when(client.prepareUpdate()).thenReturn(updateRequestBuilder);

    UpdateByQueryRequestBuilder updateByQueryRequestBuilder = mock(UpdateByQueryRequestBuilder.class);
    // when(updateByQueryRequestBuilder.source(ElasticSearchIndexConstant.ROUTE_MANAGEMENT_MAP_INDEX)).thenReturn(updateByQueryRequestBuilder);
    // when(updateByQueryRequestBuilder.script(any(Script.class))).thenReturn(updateByQueryRequestBuilder);

    when(updateByQueryRequestBuilder.execute()).thenReturn(listenableActionFuture); 

I still got same error ;(

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