# Mocking an Aggregation

**URL:** <https://discuss.elastic.co/t/mocking-an-aggregation/338363>\
**Category:** Elasticsearch\
**Created:** [July 13, 2023, 10:13pm UTC](https://discuss.elastic.co/t/mocking-an-aggregation/338363 "2023-07-13T22:13:51Z")\
**Posts on this page:** 3\
**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:** [July 13, 2023, 10:13pm UTC](https://discuss.elastic.co/t/mocking-an-aggregation/338363/1 "2023-07-13T22:13:51Z")

</div>

I'm trying to mock an Aggregation result for a Unit Test.

```auto
        var mockedSearchResponse = SearchResponse.of(r -> r
                .took(10)
                .timedOut(false)
                .hits(h -> h
                        .total(t -> t.value(8).relation(TotalHitsRelation.Eq))
                        .hits(Collections.emptyList()))
                .aggregations(Map.of(
                        "memUsagePctAvg", new Aggregate("avg", JsonData.of(0.01)),
                        "cpuTotalAvg", new Aggregate("avg", JsonData.of(0.0)))
                )
                .shards(s -> s
                        .total(1)
                        .failed(0)
                        .successful(1))
        );

```

However, there is an issue with calling new Aggregate(). It puts my "avg" kind into the \_customKind field, and sets the Aggregate \_kind to Kind.\_Custom.

Is there an example somewhere that uses the Aggregate.of() function instead.

---

<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 20, 2023, 8:14pm UTC](https://discuss.elastic.co/t/mocking-an-aggregation/338363/2 "2023-07-20T20:14:29Z")

</div>

Answering my own quetsion:

```auto
        return SearchResponse.of(r -> r
                .took(10)
                .timedOut(false)
                .hits(h -> h
                        .total(t -> t.value(5).relation(TotalHitsRelation.Eq))
                        .hits(Collections.emptyList()))
                .aggregations("cpuSystemAvg", a -> a
                        .avg(v -> v
                                .value(METRIC_VALUE)))
                .aggregations("systemLoad1", a -> a
                        .avg(v -> v
                                .value(METRIC_VALUE)))
                .aggregations("upTime", a -> a
                        .max(v -> v
                                .value(UPTIME_MSEC)))
                .shards(s -> s
                        .total(1)
                        .failed(0)
                        .successful(1))
        );

```

---

<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 17, 2023, 8:15pm UTC](https://discuss.elastic.co/t/mocking-an-aggregation/338363/3 "2023-08-17T20:15:28Z")

</div>

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