- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 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/
我为 ElasticSearch 中的文档索引编写了以下类: import java.net.InetAddress import com.typesafe.config.ConfigFactory
当我使用 TransPortClient 方法创建 ElasticSearch 客户端时。代码是这样的: public static void main(String[] args) { //
我正在使用 ElasticSearch Java 客户端来查询 Elasticsearch 。每次我必须打电话时,我都会初始化传输客户端。这是正确的方法还是我应该在应用程序启动期间初始化一次并在关闭时
我想知道在打开和关闭 java elasticsearch 客户端时有什么好的做法。我是否在每个请求之间打开和关闭它?或者我可以对所有请求使用单个客户端实例吗? private Client clie
我在为 Elasticsearch 创建 TransportClient bean 时遇到问题,编译器说构造函数是私有(private)的,就像那样。我如何才能正确创建 bean?这是我的配置类: i
我期待将 Elasticsearch 集成到 Spring Boot Web 应用程序中。这是我创建传输客户端的配置: @Configuration public class Elasticsearc
我正在使用 ElasticSearch 版本 5.6.0 我正在尝试获取 MockTransportClient 这是代码 class CampaignTest extends ESTestCase
我正在使用Maven在Java项目中使用Elasticsearch: ... 6.7.0 ... org.elasticsearch elasti
ElasticSearch Java TransportClient 5.5.1 似乎正在泄漏字节数组。即使我只是连接并关闭,注释中间的代码,它也会泄漏。 代码: try (PreBuiltTrans
我是 Elastisearch Java API 新手 我安装了版本5.6,我的服务器elasticsearch正在运行 http://localhost:9200/ { "name"
我一直在尝试在 Elastic-Search 中创建 TrasportClient,但不知何故无法运行它。 Java代码: package es_example.custom; import org.
寻求专家的帮助(我是 Elasticsearch 的新手)...有多个 Elasticsearch 节点。 我正在使用 ElasticSearch java lib 来索引 json 文档。想知道如何
我已经编写了一个带有聚合的完整查询,当我使用 REST 并使用 CURL 调用并执行 POST 时,这正是我想要的: POST http://127.0.0.1:9200/user_index/acc
我正在尝试通过 TransportClient 在 java 中使用 elasticsearch。我已经使用 logstash 将 mysql 与 elasticsearch 集成。这就是我初始化和使
有人知道什么是 Java Config 等同于: 我特别想使用 nodeBuilder() 来完成它。 最佳答案 查看 ElasticSearch 的 Spring Data 文档:
我收到此错误,我尝试增加集群实例以及执行程序和驱动程序参数的内存但没有成功。 17/05/07 23:17:07 ERROR TransportClient: Failed to send RPC 6
我已经阅读了官方文档 https://www.elastic.co/blog/found-interfacing-elasticsearch-picking-client 但它没有提供任何基准或性能数
我在 Java 中有一个简单的 elasticsearch 代码是这样的: public class TryElastic { public static void main(String[] arg
我正在 elasticsearch 中创建一个 TransportClient 实例。下面是相同的代码。问题是我正在尝试减少使用 TransportClient 启动的线程池生成的线程数。但是我使用我
更新:希望更清晰的细节和代码... 我正在尝试让我的第一个 Java 应用程序与在此节点上运行的 ElasticSearch 通信(删除了时间戳和日志级别): $ bin/elasticsearch
我是一名优秀的程序员,十分优秀!