Java 在gradle构建中启动elasticsearch以进行集成测试
在运行集成测试之前,是否有办法在gradle构建中启动elasticsearch,然后停止elasticsearch 到目前为止,我的方法如下,但这阻碍了gradle构建的进一步执行Java 在gradle构建中启动elasticsearch以进行集成测试,java,
elasticsearch,gradle,Java,
elasticsearch,Gradle,在运行集成测试之前,是否有办法在gradle构建中启动elasticsearch,然后停止elasticsearch 到目前为止,我的方法如下,但这阻碍了gradle构建的进一步执行 task runES(type: JavaExec) { main = 'org.elasticsearch.bootstrap.Elasticsearch' classpath = sourceSets.main.runtimeClasspath systemProperties = ["
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 我已经试过了,但是spring不起作用,因为它与SpringRunner不协调 我发现在before方法中启动elasticsearch更容易: 我的测试类测试一些“虚拟”生产代码(为文档编制索引):
使用elasticsearch 1.3.3或更高版本也非常重要。请参阅。您只需要在后台运行它,但这在
JavaExec中是不可能的。
Yes很遗憾,这还没有实现。这其中有一个问题:
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
}
}
}