2016-04-18 84 views
1

我想編寫測試彈性查詢構建的單元測試。我想測試某些參數值產生某些查詢。如何編寫彈性單元測試以測試查詢構建

我開始尋找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] 
+0

https://www.elastic.co/guide/en/ elasticsearch/reference/current/testing-framework.html –

+4

我希望有更多使用ESTestCase和ESIntegTestCase的代碼示例 – Aliya

回答

3

顯然有點晚了,但...

所以這實際上無關與ES測試框架,但而是你的運行設置。假設你在eclipse中運行它,這實際上是Assertions mismatch: -ea was not specified but -Dtests.asserts=true的重複。

  1. eclipse偏好 - > junit - >添加-ea複選框啓用。
  2. 右鍵點擊Eclipse項目 - >運行爲 - >運行配置 - >參數選項卡 - >添加-ea選項在VM參數