且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 spring-data-elasticsearch 在 elasticsearch 中进行集成测试?

更新时间:2023-01-11 16:59:22

您没有编写所使用的 JUnit 版本.我可以告诉你我们如何在 Spring Data Elasticsearch 中处理这个问题:

You did not write what version of JUnit you are using. I can tell you how we handle this in Spring Data Elasticsearch itself:

对于 JUnit 4,您可以查看 JUnit 4 Rule 使用 Utils class 设置本地运行的 Elasticsearch 节点并在最后将其拆除.

For JUnit 4 you can check the JUnit 4 Rule that uses the Utils class to set up a local running Elasticsearch node and tears it down at the end.

对于 JUnit 5,您可能会查看当前 master 分支中的处理方式,相关类 在这里找到.

For JUnit 5 you might have a look at how this is handled in the current master branch,the relevant classes are found here.

通过使用注释 SpringIntegrationTest 启动本地 Elasticsearch,并在所有测试完成后自动关闭.在内部,在设置集群、将信息获取到 JUnit 扩展以及启用 Spring 将相关信息自动装配到配置类方面做了相当多的工作.这个设置相当复杂,但最终它使用了与上面提到的相同的 Utils 类.

By using the annotation SpringIntegrationTest a local Elasticsearch is started and automatically shut down when all tests are done. Internally there is quite some work done in setting the cluster up, getting the info into the JUnit extension and enabling Spring autowiring of the relevant information into the configuration class. This setup is quite complex, but in the end it uses the same Utils class mentioned above.

我希望这是一个好的起点

I hope this gives a good starting point