出于我的目的,我决定在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()); } 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或更高版本也非常重要。参见问题5401。



