我正在尝试创建一个Spring Boot 2集成测试,以启动嵌入式Elasticsearch(使用JUnit 5和junit-jupiter-api)
问题是引导应用程序中的Elasticsearch Client在EmbeddedElastic
之前启动,因此无法连接。使用@Order(Ordered.HIGHEST_PRECEDENCE)
似乎无效。
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {MyApplication.class})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestPropertySource(locations = "classpath:/test.properties")
public class MyAppIT {
@Autowired
private EmbeddedElastic embeddedElastic;
@BeforeAll
public void beforeAll() throws Exception {
EmbeddedElastic elastic = embeddedElastic.start();
System.out.println("embeddedElastic running on port = " + elastic.getHttpPort());
}
@Test
public void testOne() throws Exception {
System.out.println("testOne");
}
}
@Configuration
public class TestConfiguration {
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public EmbeddedElastic embeddedElastic() {
return EmbeddedElastic.builder()
.withElasticVersion("6.6.0")
.withSetting(PopularProperties.TRANSPORT_TCP_PORT, 9300)
.withSetting(PopularProperties.CLUSTER_NAME, "test_cluster")
.withCleanInstallationDirectoryOnStop(false)
.build();
}
}