使用 mockito 在 Elastic 中模拟聚合 [英] Mocking Aggregations In Elastic using mockito
问题描述
我有一个从 searchResponse 映射产品的方法.映射时我使用全局、过滤和嵌套
I have a method which maps products from a searchResponse . While mapping I am using Global, Filter and Nested
Global globalAggregation = searchResponse.getAggregations().get(FACETS);
Filter filterAgg = globalAggregation.getAggregations().get(GENERAL_AGGREGATION);
Nested agg = filterAgg.getAggregations().get(facetAttribute);
我能够使用存根 json 为上述代码编写单元测试,这工作正常,但维护它变得困难.有没有更好的方法来为我们可以直接模拟它们的场景编写单元测试用例.
I am able to write unit test for the above code using a stub json , that works fine but maintaining it is becoming difficult. Is there a better way to write unit test cases for such scenarios where we can directly mock them .
这是我试过的.
SearchResponse searchResponse = mock(SearchResponse.class);
Aggregations aggregations = mock(Aggregations.class);
Global global= mock(Global.class);
when(searchResponse.getAggregations().get("facet")).thenReturn(global);
最后一行不行,有没有更好的处理方法?
The last line does not work , is there a better way to handle it ?
推荐答案
我认为您需要模拟 searchResponse.getAggregations()
方法调用.你可以试试这样的吗?
I think that you need to mock searchResponse.getAggregations()
method call. Can you try something like this?
when(searchResponse.getAggregations()).thenReturn(aggregations);
when(aggregations.get("facet")).thenReturn(global)
这篇关于使用 mockito 在 Elastic 中模拟聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!