如何在ElasticSearch中获取模拟TransportClient

时间:2017-10-06 10:55:48

标签: java scala elasticsearch elasticsearch-5

我正在使用ElasticSearch版本5.6.0我试图获取MockTransportClient这里是代码

class CampaignTest extends ESTestCase {
  def mockClient ={
   val clusterName = "elasticsearch"
   val dataDir = Files.createTempDirectory("elasticsearch_data_test").toFile
   val settings = Settings.builder()
    .put("path.data", dataDir.toString)
    .put("cluster.name", clusterName)
    .build

   val client = new MockTransportClient(settings);
  println("got the client "+client)  
  client
  }
}

class TestCampaign extends PlaySpec {
  val campaignTest= new CampaignTest
  val client = campaignTest.mockClient
  info("client is "+client)
}

我尝试运行TestCampaign

时遇到异常
[2017-10-06T15:51:55,191][INFO ][o.e.p.PluginsService     ] [_client_] no modules loaded
[2017-10-06T15:51:55,206][INFO ][o.e.p.PluginsService     ] [_client_] no plugins loaded
[info] DeferredAbortedSuite:
[info] Exception encountered when attempting to run a suite with class name: org.scalatest.DeferredAbortedSuite *** ABORTED ***
[info]   java.lang.IllegalStateException: running tests but failed to invoke RandomizedContext#getRandom
[info]   at org.elasticsearch.common.Randomness.get(Randomness.java:105)
[info]   at org.elasticsearch.client.transport.TransportClientNodesService.<init>(TransportClientNodesService.java:100)
[info]   at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:195)
[info]   at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:265)
[info]   at org.elasticsearch.transport.MockTransportClient.<init>(MockTransportClient.java:42)
[info]   at org.elasticsearch.transport.MockTransportClient.<init>(MockTransportClient.java:34)
[info]   at testcontrollers.campaign.CampaignTest.mockClient(CampaignTest.scala:34)
[info]   at testcontrollers.campaign.TestCampaign.<init>(TestCampaign.scala:13)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[info]   ...
[info]   Cause: java.lang.reflect.InvocationTargetException:
[info]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[info]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[info]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[info]   at java.lang.reflect.Method.invoke(Method.java:498)
[info]   at org.elasticsearch.common.Randomness.get(Randomness.java:101)
[info]   at org.elasticsearch.client.transport.TransportClientNodesService.<init>(TransportClientNodesService.java:100)
[info]   at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:195)
[info]   at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:265)
[info]   at org.elasticsearch.transport.MockTransportClient.<init>(MockTransportClient.java:42)
[info]   at org.elasticsearch.transport.MockTransportClient.<init>(MockTransportClient.java:34)
[info]   ...
[info]   Cause: java.lang.IllegalStateException: No context information for thread: Thread[id=10, name=pool-1-thread-1, state=RUNNABLE, group=main]. Is this thread running under a class com.carrotsearch.randomizedtesting.RandomizedRunner runner context? Add @RunWith(class com.carrotsearch.randomizedtesting.RandomizedRunner.class) to your test class. Make sure your code accesses random contexts within @BeforeClass and @AfterClass boundary (for example, static test class initializers are not permitted to access random contexts).
[info]   at com.carrotsearch.randomizedtesting.RandomizedContext.context(RandomizedContext.java:248)
[info]   at com.carrotsearch.randomizedtesting.RandomizedContext.current(RandomizedContext.java:134)
[info]   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[info]   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[info]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[info]   at java.lang.reflect.Method.invoke(Method.java:498)
[info]   at org.elasticsearch.common.Randomness.get(Randomness.java:101)
[info]   at org.elasticsearch.client.transport.TransportClientNodesService.<init>(TransportClientNodesService.java:100)
[info]   at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:195)
[info]   at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:265)

以下是build.sbt中的依赖项

"org.elasticsearch" % "elasticsearch" % "5.6.0",
                "org.elasticsearch.client" % "transport" % "5.6.0",
                "org.apache.lucene" % "lucene-expressions" % "6.6.0",
                "org.apache.logging.log4j" % "log4j-core" % "2.9.0",
                "org.apache.lucene" % "lucene-test-framework" % "6.6.0" % "test" ,
                "org.elasticsearch.test" % "framework" % "5.5.2" % "test",

我犯错的地方

1 个答案:

答案 0 :(得分:0)

因为 elasticsearch 测试取决于randomizedtesting,并且需要与RandomizedRunner一起注入random context。因此,对于您的示例,您应该在RandomizedRunner上下文下运行测试。

由于RandomizedRunner只支持JUnit运行,因此对于您的测试应该只支持class CampaignTest extends ESTestCase而不是PlaySpec JUnit,您可以使用JUnit语法编写测试,例如:

class CampaignTest extends ESTestCase {
  var client: Client = null


  @Before def initClient(): Unit = { 
    val file = Files.createTempDirectory("tempESData")
    val settings = Settings.builder()
      .put("http.enabled", "false")
      .put("path.data", file.toString()).build()
    client = new MockTransportClient(settings)
  }
  @Test def myTest(): unit = {
    ...
  }

  @After def closeClient(): Unit = {
    client.close() //close client after test
    client = null
  }
}