gpt4 book ai didi

java - 在 gradle build 中启动 elasticsearch 以进行集成测试

转载 作者:行者123 更新时间:2023-11-29 05:16:07 26 4
gpt4 key购买 nike

有没有办法在运行集成测试之前在 gradle 构建中启动 elasticsearch,然后停止 elasticsearch?

到目前为止,我的方法如下,但这会阻止 gradle 构建的进一步执行。

task runES(type: JavaExec) {
main = 'org.elasticsearch.bootstrap.Elasticsearch'
classpath = sourceSets.main.runtimeClasspath
systemProperties = ["es.path.home":"$buildDir/elastichome",
"es.path.data":"$buildDir/elastichome/data"]
}

最佳答案

为了我的目的,我决定在我的 java 代码集成测试中启动 elasticsearch。

我试过了 ElasticsearchIntegrationTest但这对 spring 不起作用,因为它与 SpringJUnit4ClassRunner 不协调。

我发现在 before 方法中启动 elasticsearch 更容易:

我的测试类测试一些“虚拟”生产代码(索引文档):

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.ImmutableSettings.Builder;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.indices.IndexAlreadyExistsException;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MyIntegrationTest {

private Node node;
private Client client;

@Before
public void before() {
createElasticsearchClient();
createIndex();
}

@After
public void after() {
this.client.close();
this.node.close();
}

@Test
public void testSomething() throws Exception {
// do something with elasticsearch
final String json = "{\"mytype\":\"bla\"}";
final String type = "mytype";
final String id = index(json, type);
assertThat(id, notNullValue());
}

/**
* some productive code
*/
private String index(final String json, final String type) {
// create Client
final Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "mycluster").build();
final TransportClient tc = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(
"localhost", 9300));

// index a document
final IndexResponse response = tc.prepareIndex("myindex", type).setSource(json).execute().actionGet();
return response.getId();
}

private void createElasticsearchClient() {
final NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();
final Builder settingsBuilder = nodeBuilder.settings();
settingsBuilder.put("network.publish_host", "localhost");
settingsBuilder.put("network.bind_host", "localhost");
final Settings settings = settingsBuilder.build();
this.node = nodeBuilder.clusterName("mycluster").local(false).data(true).settings(settings).node();
this.client = this.node.client();
}

private void createIndex() {
try {
this.client.admin().indices().prepareCreate("myindex").execute().actionGet();
} catch (final IndexAlreadyExistsException e) {
// index already exists => we ignore this exception
}
}
}

使用elasticsearch 1.3.3或更高版本也很重要。参见 Issue 5401 .

关于java - 在 gradle build 中启动 elasticsearch 以进行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489172/

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