我正在使用spring-data-elasticsearch v3.2.4.RELEASE,可通过spring-boot-starter-data-elasticsearch v2.2.4.RELEASE获得。我想为此进行集成测试,但可用的选项是:https://github.com/allegro/embedded-elasticsearch不起作用。
我尝试以POC入门的部分在下面,它引发异常:
public class EmbeddedElasticConfiguration {
public static final String VERSION = "6.8.4";
public static final String DOWNLOAD_DIRECTORY = "<path>\\test-elasticsearch";
public static final String INSTALLATION_DIRECTORY = "<path>\test-elasticsearch";
public static final String NAME = "elasticsearch";
public static final String TRANSPORT_PORT = "9300";
public static final String HTTP_CLIENT_PORT = "9200";
public static final String TEST_INDEX = "salesorder";
public static final String TEST_TYPE = "salesorder";
public static final String RESOURCE_LOCATION = "src/test/resources/salesorder-mapping.json";
private ObjectMapper objectMapper = new ObjectMapper();
EmbeddedElastic embeddedElastic;
@Test
public void configure() throws IOException, InterruptedException {
embeddedElastic = EmbeddedElastic.builder()
.withElasticVersion(VERSION)
.withSetting(TRANSPORT_TCP_PORT, 9300)
.withSetting(CLUSTER_NAME, "my-cluster")
//.withPlugin("analysis-stempel")
.withDownloadDirectory(new File(DOWNLOAD_DIRECTORY))
.withInstallationDirectory(new File(INSTALLATION_DIRECTORY))
.withSetting(HTTP_PORT, 9200)
.withIndex(TEST_INDEX, IndexSettings.builder()
.withType(TEST_TYPE, readMappingFromJson())
.build())
.build();
embeddedElastic.start();
}
private String readMappingFromJson() throws IOException {
final File file = ResourceUtils.getFile(RESOURCE_LOCATION);
String mapping = new String(Files.readAllBytes(file.toPath()));
System.out.println("mapping: "+ mapping);
return mapping;
}
@After
public void stopServer() {
embeddedElastic.stop();
}
}
我在下面已成为例外:
pl.allegro.tech.embeddedelasticsearch.EmbeddedElasticsearchStartupException: Failed to start elasticsearch within time-out
at pl.allegro.tech.embeddedelasticsearch.ElasticServer.waitForElasticToStart(ElasticServer.java:127)
at pl.allegro.tech.embeddedelasticsearch.ElasticServer.start(ElasticServer.java:50)
at pl.allegro.tech.embeddedelasticsearch.EmbeddedElastic.startElastic(EmbeddedElastic.java:82)
at pl.allegro.tech.embeddedelasticsearch.EmbeddedElastic.start(EmbeddedElastic.java:63)
at com.xxx.elasticsearch.adapter.configuration.EmbeddedElasticConfiguration.configure(EmbeddedElasticConfiguration.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
有人可以在弹性搜索中使用spring-data进行集成测试的任何其他选项提供帮助,或者我应该如何为Elasticsearch编写集成测试。
我知道在stackoverflow上还有其他针对嵌入式Elasticsearch的门户的答案,但是这些不适用于我当前的Elasticsearch版本。
您未编写所使用的JUnit版本。我可以告诉您如何在Spring Data Elasticsearch本身中处理此问题:
对于JUnit 4,您可以检查使用JUnit 4 Rule来设置本地运行的Elasticsearch节点并在最后将其拆除的Utils class。>
对于JUnit 5,您可能会看一下如何在当前master分支(相关类are found here)中处理此问题。
通过使用批注SpringIntegrationTest
,本地Elasticsearch将启动,并在完成所有测试后自动关闭。在内部,在设置集群,将信息放入JUnit扩展以及启用Spring将相关信息自动装配到配置类中方面,已经完成了许多工作。此设置非常复杂,但最终它使用上面提到的相同的Utils
类。
我希望这是一个很好的起点