如何编写弹性单元测试来测试查询构建

时间:2016-04-18 19:11:24

标签: unit-testing elasticsearch

我想编写测试Elastic查询构建的单元测试。我想测试某些参数值会产生某些查询。

我开始研究ESTestCase。我看到你可以使用ESTestCase模拟一个客户端。我真的不需要模拟ES节点,我只需要重现查询构建部分,但这需要客户端。

有人处理过这样的问题吗?

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.google.common.collect.Lists;

public class SearchRequestBuilderTests extends ESTestCase {
    private static Client client;

    @BeforeClass
    public static void initClient() {
        //this client will not be hit by any request, but it needs to be a non null proper client
        //that is why we create it but we don't add any transport address to it
        Settings settings = Settings.builder()
            .put("", createTempDir().toString())
            .build();

        client = TransportClient.builder().settings(settings).build();
    }

    @AfterClass
    public static void closeClient() {
        client.close();
        client = null;
    }

    public static Map<String, String> createSampleSearchParams() {
        Map<String, String> searchParams = new HashMap<>();
        searchParams.put(SenseneConstants.ADC_PARAM, "US");
        searchParams.put(SenseneConstants.FETCH_SIZE_QUERY_PARAM, "10");
        searchParams.put(SenseneConstants.QUERY_PARAM, "some query");
        searchParams.put(SenseneConstants.LOCATION_QUERY_PARAM, "");
        searchParams.put(SenseneConstants.RADIUS_QUERY_PARAM, "20");
        searchParams.put(SenseneConstants.DISTANCE_UNIT_PARAM, DistanceUnit.MILES.name());
        searchParams.put(SenseneConstants.GEO_DISTANCE_PARAM, "true");

        return searchParams;
     }

    @Test
    public void test() {
        BasicSearcher searcher = new BasicSearcher(client); // this is my application's searcher
        Map<String, String> searchParams = createSampleSearchParams();

        ArrayList<String> filterQueries = Lists.newArrayList();
        SearchRequest searchRequest = SearchRequest.create(searchParams, filterQueries);

        MySearchRequestBuilder medleyReqBuilder = new MySearchRequestBuilder.Builder(client, "my_index", searchRequest).build();
        SearchRequestBuilder searchRequestBuilder = medleyReqBuilder.constructSearchRequestBuilder();
        System.out.print(searchRequestBuilder.toString());
    // Here I want to assert that the search request builder output is what it should be for the above client params    
    }
}

我明白了,代码中没有任何东西运行:

 Assertions mismatch: -ea was not specified but -Dtests.asserts=true
 REPRODUCE WITH: mvn test -Pdev -Dtests.seed=5F09BEDD71BBD14E -      Dtests.class=SearchRequestBuilderTests -Dtests.locale=en_US -Dtests.timezone=America/Los_Angeles
 NOTE: test params are: codec=null, sim=null, locale=null, timezone=(null)
 NOTE: Mac OS X 10.10.5 x86_64/Oracle Corporation 1.7.0_80 (64-bit)/cpus=4,threads=1,free=122894936,total=128974848
 NOTE: All tests run in this JVM: [SearchRequestBuilderTests]

1 个答案:

答案 0 :(得分:3)

显然有点晚了但是......

所以这实际上与ES测试框架无关,而是与您的运行设置无关。假设你在eclipse中运行它,这实际上是Assertions mismatch: -ea was not specified but -Dtests.asserts=true的副本。

  1. eclipse偏好 - &gt; junit - &gt;添加-ea复选框启用。
  2. 右键点击eclipse项目 - &gt;以 - &gt;运行运行配置 - &gt;参数选项卡 - &gt;在vm arguments
  3. 中添加-ea选项