如何模拟Elasticsearch Java客户端?
来源:爱站网时间:2021-09-16编辑:网友分享
您知道如何正确模拟Elasticsearch Java Client吗?当前正在模拟Java中的以下请求:SearchResponse response = client.prepareSearch(index).setTypes(type)...
问题描述
您知道如何正确模拟Elasticsearch Java Client吗?当前模拟Java中的以下请求:
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setFrom(0).setSize(MAX_SIZE)
.execute()
.actionGet();
SearchHit[] hits = response.getHits().getHits();
我必须嘲笑:
- client.prepareSearch
-
SearchRequestBuilder:
- builder.execute
- builder.setSize
- builder.setFrom
- builder.setTypes
-
SearchResponse:
- action.actionGet
-
SearchResponse:
- response.getHits
- searchHits.getHits
所以我的测试看起来像:
SearchHit[] hits = ..........;
SearchHits searchHits = mock(SearchHits.class);
when(searchHits.getHits()).thenReturn(hits);
SearchResponse response = mock(SearchResponse.class);
when(response.getHits()).thenReturn(searchHits);
ListenableActionFuture action = mock(ListenableActionFuture.class);
when(action.actionGet()).thenReturn(response);
SearchRequestBuilder builder = mock(SearchRequestBuilder.class);
when(builder.setTypes(anyString())).thenReturn(builder);
when(builder.setFrom(anyInt())).thenReturn(builder);
when(builder.setSize(anyInt())).thenReturn(builder);
when(builder.execute()).thenReturn(action);
when(client.prepareSearch(index)).thenReturn(builder);
丑...因此,我想知道是否还有一种“优雅的方式”来模拟此代码。
谢谢
思路:
[我在模拟构建器时遇到了类似的问题,所以我想我会尝试看看是否有更好的方法。
正如Spoon先生所说,如果您一开始就避免这样做,那可能会更好,因为它不是您的代码,并且可以假定为“工作正常”,但我想我还是会尝试一下。 >
我已经通过在Mockito中使用“默认答案”提出了一种(也许是粗略的)方法。我仍在决定是否喜欢。
这是我的建造者...
public class MyBuilder { private StringBuilder my; public MyBuilder() { my = new StringBuilder(); } public MyBuilder name(String name) { my.append("[name=").append(name).append("]"); return this; } public MyBuilder age(String age) { my.append("[age=").append(age).append("]"); return this; } public String create() { return my.toString(); } }
(非常基本的权利?)
我的测验看起来像这样...
// Create a "BuilderMocker" (any better name suggestions welcome!) BuilderMocker
mocker = BuilderMocker.forClass(MyBuilder.class); // Get the actual mock builder MyBuilder builder = mocker.build(); // expect this chain of method calls... mocker.expect().name("[NAME]").age("[AGE]"); // expect this end-of-chain method call... Mockito.when(builder.create()).thenReturn("[ARGH!]"); 现在,如果我执行以下操作...
System.out.println(builder.name("[NAME]").age("[AGE]").create());
...我希望输出“ [ARGH!]”。
如果我更改了最后一行...
System.out.println(builder.name("[NOT THIS NAME]").age("[AGE]").create());
...然后我希望它会因NullPointerException而中断。
这里是实际的“ BuilderMocker” ...
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.Mockito.withSettings; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; public class BuilderMocker
{ private Class clazz; private T recorder; private T mock; // Create a BuilderMocker for the class public static BuilderMocker forClass(Class clazz) { return new BuilderMocker (clazz); } private BuilderMocker(Class clazz) { this.clazz = clazz; this.mock = mock(clazz); createRecorder(); } // Sets up the "recorder" private void createRecorder() { recorder = mock(clazz, withSettings().defaultAnswer(new Answer 不确定在Mockito中是否有“内置”方法来执行此操作,但这似乎可行。