# Mocking Search Results in new Java API

**URL:** <https://discuss.elastic.co/t/mocking-search-results-in-new-java-api/337012>\
**Category:** Elasticsearch\
**Created:** [June 27, 2023, 3:34pm UTC](https://discuss.elastic.co/t/mocking-search-results-in-new-java-api/337012 "2023-06-27T15:34:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![silentfilm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silentfilm/32/44348_2.png) [@silentfilm](https://discuss.elastic.co/u/silentfilm)\
**Post date:** [June 27, 2023, 3:34pm UTC](https://discuss.elastic.co/t/mocking-search-results-in-new-java-api/337012/1 "2023-06-27T15:34:05Z")

</div>

Are there any examples of how to mock an Elasticsearch search result for the Java API for unit tests with Mockito? Do you mock the entire search result or individual hits? If I search the Internet for examples I only see the old Rest Client examples. Can you use an input stream to manually create your search response?

Someone has previously asked [here](https://discuss.elastic.co/t/mockito-test-for-elastic-java-api-client/315773), but there was no answer to their question. There are no examples that show how to create a manual SearchResponse\<\> that I can find.

---

<div class="post-metadata">

**Author:** ![silentfilm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silentfilm/32/44348_2.png) [@silentfilm](https://discuss.elastic.co/u/silentfilm)\
**Post date:** [July 6, 2023, 7:24pm UTC](https://discuss.elastic.co/t/mocking-search-results-in-new-java-api/337012/2 "2023-07-06T19:24:29Z")

</div>

Surely somebody has done this before. It was not that difficult to do in the old REST client, but the SearchResponse structure is different now. I've tried something like:

```auto
        ElasticsearchClient client = mock(ElasticsearchClient.class);
        // Set up mocked results
        var response = new SearchResponse.Builder().build().hits().hits().add(0,
                new ESMetrics("aln-nbadev4.labs.server.com", NAME_FIELD_VALUE, SPARK_MASTER_VALUE,
                        "aln-nbadev4.labs.server.com", 0L));

```

However, I get: "MissingRequiredProperty 'SearchResponse.took'". The idea is to mock in a way similar to this:

```auto
        when(client.search((SearchRequest) any(), ESMetrics.class)).
                thenReturn(response);

```

---

<div class="post-metadata">

**Author:** ![silentfilm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silentfilm/32/44348_2.png) [@silentfilm](https://discuss.elastic.co/u/silentfilm)\
**Post date:** [July 6, 2023, 9:44pm UTC](https://discuss.elastic.co/t/mocking-search-results-in-new-java-api/337012/3 "2023-07-06T21:44:32Z")

</div>

OK, I think that I've figured this out. I'm posting this here for future reference.

```auto
    SearchResponse<ESMetrics> createMockedResponse (long value) {

        return SearchResponse.of(r -> r
                .took(0)
                .timedOut(false)
                .hits(h -> h
                        .total(t -> t.value(1).relation(TotalHitsRelation.Eq))
                        .hits(hit -> hit
                                .id("1234")
                                .index("metrics-7.17.9-2023.07.06")
                                .fields(Map.of("hostname", JsonData.of("aln-nbadev4.labs.server.com"),
                                        "@timestamp", JsonData.of("2020-09-17T20:23:55.382Z"),
                                        "field_name", JsonData.of("string_value"),
                                        "value", JsonData.of(value))))
                )
                .shards(s -> s
                        .total(1)
                        .failed(0)
                        .successful(1)
                )
        );

```

---

<div class="post-metadata">

**Author:** ![silentfilm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/silentfilm/32/44348_2.png) [@silentfilm](https://discuss.elastic.co/u/silentfilm)\
**Post date:** [July 7, 2023, 9:30pm UTC](https://discuss.elastic.co/t/mocking-search-results-in-new-java-api/337012/4 "2023-07-07T21:30:45Z")

</div>

The example above did not completely work because a call to .source() always returned null. You have to define the .source() structure also:

```auto
    SearchResponse<ESMetrics> createMockedResponse(long value) {

        ESMetrics entry = new ESMetrics("aln-nbadev4.labs.server.com",
                NAME_FIELD_VALUE, SOURCE_FIELD_VALUE);
        entry.setValue(value);

        return SearchResponse.of(r -> r
                .took(0)
                .timedOut(false)
                .hits(h -> h
                        .total(t -> t.value(1).relation(TotalHitsRelation.Eq))
                        .hits(hit -> hit
                                .id("1234")
                                .index("metrics-7.17.9-2023.07.06")
                                .fields(Map.of("hostname", JsonData.of("aln-nbadev4.labs.server.com"),
                                        "@timestamp", JsonData.of("2020-09-17T20:23:55.382Z"),
                                         NAME_FIELD_NAME, JsonData.of(NAME_FIELD_VALUE),
                                        SOURCE_FIELD_NAME, JsonData.of(SOURCE_FIELD_VALUE),
                                        VALUE_FIELD, JsonData.of(value)))
                                .source(entry))
                )
                .shards(s -> s
                        .total(1)
                        .failed(0)
                        .successful(1)
                )
        );
    }

```

Then you can mock it like so:

```auto
        SearchResponse<ESMetrics> searchResponse = createMockedResponse(1);
        ElasticsearchClient client = mock(ElasticsearchClient.class);
        when(client.search((Function<Builder, ObjectBuilder<SearchRequest>>) any(), any())).thenReturn((SearchResponse) searchResponse);

```

It is also very important when defining your class that holds the Elasticsearch data that you are reading that if you are using Lombock notations you must use @NoArgsConstructor if any constructors are defined:

```auto
    @Data
    @RequiredArgsConstructor
    @NoArgsConstructor(force=true)
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class ESMetrics {

```

---

<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 4, 2023, 9:31pm UTC](https://discuss.elastic.co/t/mocking-search-results-in-new-java-api/337012/5 "2023-08-04T21:31:00Z")

</div>

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