如何模拟Elasticsearch Java客户端?

时间:2015-03-31 12:56:48

标签: java unit-testing elasticsearch mocking mockito

你知道如何有效地模拟Elasticsearch Java客户端吗?目前在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<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);

...丑 所以我想知道是否有更“优雅的方式”来模拟这段代码。

由于

1 个答案:

答案 0 :(得分:2)

我在嘲笑建筑商时遇到过类似的问题,所以我想我会尝试看看是否有更好的方式。

正如斯潘先生所说,如果你可以避免这样做,可能会更好,因为它不是你的代码,可以假设为“只是工作”,但我想无论如何,我还是试一试。

我通过使用&#34;默认答案&#34;来提出一种(可能粗略的)方法。在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());

......我期待&#34; [ARGH!]&#34;输出。

如果我改变了最后一行......

    System.out.println(builder.name("[NOT THIS NAME]").age("[AGE]").create());

...然后我希望它会因NullPointerException而中断。

这是实际的&#34; BuilderMocker&#34; ...

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;
    }
}

不确定是否有&#34;内置&#34;在Mockito这样做的方式,但这似乎有效。