如何使用嵌入式ElasticSearch进行集成测试

时间:2017-08-23 12:35:15

标签: java scala elasticsearch integration-testing elasticsearch-1.6.0

我正在关注本教程

  1. https://orrsella.com/2014/10/28/embedded-elasticsearch-server-for-scala-integration-tests/
  2. 我正在尝试关注该链接,因为我使用的是ELasticSearch 1.6.0,但它使用的是NodeClient,我想使用transportClient但我得到了异常

     org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
    [info]   at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:305)
    [info]   at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:200)
    [info]   at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.java:86)
    [info]   at org.elasticsearch.client.support.AbstractIndicesAdminClient.create(AbstractIndicesAdminClient.java:367)
    [info]   at org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder.doExecute(CreateIndexRequestBuilder.java:250)
    [info]   at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
    [info]   at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
    [info]   at testcontrollers.campaign.ElasticsearchServer.createAndWaitForIndex(ElasticsearchServer.scala:40)
    [info]   at testcontrollers.campaign.CampaignTestSearch.<init>(CampaignTestSearch.scala:41)
    [info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    

    这是代码

    class ElasticsearchServer {
      val log = LoggerFactory.getLogger(this.getClass)
      private val clusterName = "testcluster"
      private val dataDir = Files.createTempDirectory("elasticsearch_test_data_").toFile
      private val settings = ImmutableSettings.settingsBuilder
        .put("path.data", dataDir.toString)
        .put("cluster.name", clusterName)
        .put("node.local",true)
        .build
    
      def client: TransportClient ={
        val client=new TransportClient(settings)
        .addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300))
       client
      }
      def stop(): Unit = {
        client.close();
    
        try {
          FileUtils.forceDelete(dataDir)
        } catch {
          case e: Exception => // dataDir cleanup failed
        }
      }
    
        def createAndWaitForIndex(index: String): Unit = {
          client.admin.indices.prepareCreate(index).execute.actionGet()
          client.admin.cluster.prepareHealth(index).setWaitForActiveShards(1).execute.actionGet()
        }
    }
    
    class CampaignTestSearch extends PlaySpec{
      val server = new ElasticsearchServer
      server.createAndWaitForIndex("arteciatetestdb")
    val client=server.client
    
    val response = client.prepareSearch("dbtest")
          .setTypes(CAMPAIGN_COLLECTION_NAME)
          .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
          .addFields("uuid","campaignName","artworkID","activationDate","_source")
          .setQuery(query)
          .execute()
          .actionGet()
      }
    

2 个答案:

答案 0 :(得分:2)

首先,您需要记住,较新版本的elasticsearch已经放弃了对嵌入式使用它的支持,其原因在this blog post中有描述。

在弹性搜索的许多版本中考虑到这一点,包括5,您可以使用ESIntegTestCase支持类,并使您的集成测试类扩展它,以便在测试中支持嵌入式弹性搜索。您可以在their official documentation处详细了解此信息。

来自他们的官方文件:

public class Mytests extends ESIntegTestCase {

  @Override
  protected Settings nodeSettings(int nodeOrdinal) {
      return Settings.builder().put(super.nodeSettings(nodeOrdinal))
             .put("node.mode", "network")
             .build();
  }

}

在版本1.x中有一个名为ElasticsearchTestCase的类,它将支持弹性单元测试,检查this link on their official repository以获取有关如何使用它的完整示例。

答案 1 :(得分:0)

不再支持嵌入式Elasticsearch

您可以使用此Maven依赖项,它将为您启动Elasticsearch 6集群

<dependency>
    <groupId>org.elasticsearch-6</groupId>
    <artifactId>elasticsearch-embedded-cluster</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

您可以阅读有关的更多详细信息 https://github.com/nitishgoyal13/elasticsearch-6-embedded-cluster