gpt4 book ai didi

java - 如何在 ElasticSearch 中获取模拟 TransportClient

转载 作者:太空宇宙 更新时间:2023-11-04 11:04:23 27 4
gpt4 key购买 nike

我正在使用 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 测试取决于 randomizedtesting并且需要使用 RandomizedRunner 运行来注入(inject)随机上下文。因此,对于您的示例,您应该在 RandomizedRunner 上下文下运行测试。

由于 RandomizedRunner 只能支持使用 JUnit 运行,因此您的测试应该仅支持使用 JUnit 运行 class CampaignTest extends ESTestCase 而不是 PlaySpec ,并且您可以使用 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
}
}

关于java - 如何在 ElasticSearch 中获取模拟 TransportClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46604167/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com