您知道如何对Elasticsearch Java客户端进行属性模拟吗?目前在Java中模拟以下请求:
SearchResponse response = client.prepareSearch(index)
.setTypes(type)
.setFrom(0).setSize(MAX_SIZE)
.execute()
.actionGet();
SearchHit[] hits = response.getHits().getHits();
我不得不嘲笑:
- client.prepareSearch
- 搜索请求生成器:
- builder.execute
- builder.setSize
- builder.setFrom
- builder.setTypes
- 搜索响应:
- action.actionGet
- 搜索响应:
- 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<SearchResponse> 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<MyBuilder> 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<T> {
private Class<T> clazz;
private T recorder;
private T mock;
// Create a BuilderMocker for the class
public static <T> BuilderMocker<T> forClass(Class<T> clazz) {
return new BuilderMocker<T>(clazz);
}
private BuilderMocker(Class<T> clazz) {
this.clazz = clazz;
this.mock = mock(clazz);
createRecorder();
}
// Sets up the "recorder"
private void createRecorder() {
recorder = mock(clazz, withSettings().defaultAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
// If it is a chained method...
if (invocation.getMethod().getReturnType().equals(clazz)) {
// Set expectation on the "real" mock...
when(invocation.getMethod().invoke(mock, invocation.getArguments())).thenReturn(mock);
return recorder;
}
return null;
}
}));
}
// Use this to "record" the expected method chain
public T expect() {
return recorder;
}
// Use this to get the "real" mock...
public T build() {
return mock;
}
}
不确定Mockito中是否有"内置"的方法来实现这一点,但这似乎有效。