Spring Test Elasticsearch
How to write integration tests on Spring Boot with Elasticsearch in docker
Add this library in dependencies:
<dependency>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-test-elasticsearch</artifactId>
<version>0.1</version>
</dependency>
Now, you can start the Elasticsearch cluster in a docker container(TestContainers) by the using of @ElasticsearchTestContainer
annotation in tests:
@ElasticsearchTestContainer
@SpringBootTest
class SpTestElasticTestAppApplicationTests {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@BeforeAll
static void setUp() {
// populate a data set in index
}
@Test
void findTest() {
var query = GetQuery.getById("100123");
Foo foo = elasticsearchTemplate.queryForObject(query, Foo.class);
assertThat(foo).isNotNull();
}
}