# UpdateByQueryRequestBuilder unit test

**URL:** <https://discuss.elastic.co/t/updatebyqueryrequestbuilder-unit-test/142716>\
**Category:** Elasticsearch\
**Created:** [August 2, 2018, 8:25am UTC](https://discuss.elastic.co/t/updatebyqueryrequestbuilder-unit-test/142716 "2018-08-02T08:25:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dondon](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dondon](https://discuss.elastic.co/u/dondon)\
**Post date:** [August 2, 2018, 8:25am UTC](https://discuss.elastic.co/t/updatebyqueryrequestbuilder-unit-test/142716/1 "2018-08-02T08:25:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 2, 2018, 8:58am UTC](https://discuss.elastic.co/t/updatebyqueryrequestbuilder-unit-test/142716/2 "2018-08-02T08:58:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dondon](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dondon](https://discuss.elastic.co/u/dondon)\
**Post date:** [August 2, 2018, 9:18am UTC](https://discuss.elastic.co/t/updatebyqueryrequestbuilder-unit-test/142716/3 "2018-08-02T09:18:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 2, 2018, 9:33am UTC](https://discuss.elastic.co/t/updatebyqueryrequestbuilder-unit-test/142716/4 "2018-08-02T09:33:40Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![dondon](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dondon](https://discuss.elastic.co/u/dondon)\
**Post date:** [August 2, 2018, 10:29am UTC](https://discuss.elastic.co/t/updatebyqueryrequestbuilder-unit-test/142716/5 "2018-08-02T10:29:02Z")

</div>

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 ;(

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 30, 2018, 10:29am UTC](https://discuss.elastic.co/t/updatebyqueryrequestbuilder-unit-test/142716/6 "2018-08-30T10:29:04Z")

</div>

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