[英]how to get the mock TransportClient in ElasticSearch
我正在使用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",
我在哪里犯错
因为elasticsearch测试依赖于随机测试,并且需要与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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.