当前位置: 首页 > 面试题库 >

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

赵浩邈
2023-03-14
问题内容

有没有办法在运行集成测试之前在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或更高版本也非常重要。参见问题5401。



 类似资料:
  • 下面是我试图作为POC开始的部分,它抛出异常: 我在以下得到以下例外: 是否有人可以帮助使用spring-data在elasticsearch中进行集成测试的任何其他选项,或者我应该如何为elasticsearch编写集成测试。 我知道在stackoverflow和embedded-elasticsearch的其他门户上还有其他的答案,但这些都不适合我当前的elasticsearch版本。

  • 问题内容: 我想要对Maven项目进行完全自动化的集成测试。集成测试要求在运行之前启动外部(依赖于平台)程序。理想情况下,在单元测试完成后将终止外部程序,但这不是必需的。 有Maven插件可以完成此操作吗?还有其他想法吗? 问题答案: 您可以使用antrun插件。在内部,您将使用ant的 exec apply 任务。 这样的事情。 蚂蚁当然可以通过 条件任务 来支持特定的命令。

  • Gradle4.6增加了对JUnit5的支持。 只要我没有用于例如集成测试的另一个sourceset,这对我是有效的:我不知道如何在我的集成测试中启用。 我所能做的是让任务与新的JUnit5支持一起工作,但我的任务使用JUnit5控制台并从命令行运行测试。最后,我在gradle中放弃对JUnit5的支持,并回滚到使用JUnit5控制台进行两个测试。 除了之外,如何在其他任务上启用Gradle4.6

  • 我试图为一个Spring引导项目写一个集成测试。不幸的是,我对实现感到困惑。 下面是已尝试的示例代码段 问题 我是否需要一个单独的,带有注释以支持集成测试

  • 我正在测试我的spring boot应用程序。我正在做集成测试,但由于url返回的响应无法jsonfied,测试一直失败。请帮忙。以下是测试功能: 错误:

  • 我有一个Spring Boot集成测试,其类定义如下。 我得到以下例外: 我的TestApplication类定义如下: properties-context.xml定义如下: 这从一个名为external.properties.的文件中读取,在我的外部属性文件中设置了属性'spring.main.allow-Bean-定义-覆盖=true'。我添加此项是因为当我运行应用程序时,出现了相同的异常,