栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在gradle构建中启动Elasticsearch以进行集成测试

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在gradle构建中启动Elasticsearch以进行集成测试

出于我的目的,我决定在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。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371336.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号