Skip to content

Commit 8c248f6

Browse files
authored
use ServiceConnection in examples' tests (#444)
the tests of the examples can and should use `ServiceConnection` rather than manually setting the properties. this shows the correct way of using them with `Testcontainers`. Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com>
1 parent b5381a5 commit 8c248f6

File tree

2 files changed

+6
-26
lines changed

2 files changed

+6
-26
lines changed

spring-data-opensearch-examples/spring-boot-gradle/src/test/java/org/opensearch/data/example/repository/MarketplaceRepositoryIntegrationTests.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,24 @@
1313
import org.opensearch.spring.boot.autoconfigure.test.DataOpenSearchTest;
1414
import org.opensearch.testcontainers.OpensearchContainer;
1515
import org.springframework.beans.factory.annotation.Autowired;
16-
import org.springframework.boot.test.util.TestPropertyValues;
17-
import org.springframework.context.ApplicationContextInitializer;
18-
import org.springframework.context.ConfigurableApplicationContext;
16+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
1917
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
20-
import org.springframework.test.context.ContextConfiguration;
2118
import org.testcontainers.junit.jupiter.Container;
2219
import org.testcontainers.junit.jupiter.Testcontainers;
2320

2421
@Testcontainers(disabledWithoutDocker = true)
2522
@DataOpenSearchTest
2623
@EnableElasticsearchRepositories(basePackageClasses = MarketplaceRepository.class)
27-
@ContextConfiguration(initializers = MarketplaceRepositoryIntegrationTests.Initializer.class)
2824
@Tag("integration-test")
2925
public class MarketplaceRepositoryIntegrationTests {
3026
@Container
27+
@ServiceConnection
3128
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.19.1")
3229
.withStartupAttempts(5)
3330
.withStartupTimeout(Duration.ofMinutes(2));
3431

3532
@Test
3633
void testMarketplaceRepository(@Autowired MarketplaceRepository repository) {
37-
assertThat(repository.findAll()).hasSize(0);
38-
}
39-
40-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
41-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
42-
TestPropertyValues.of("opensearch.uris=" + opensearch.getHttpHostAddress())
43-
.applyTo(configurableApplicationContext.getEnvironment());
44-
}
34+
assertThat(repository.findAll()).isEmpty();
4535
}
4636
}

spring-data-opensearch-examples/spring-boot-java-client-gradle/src/test/java/org/opensearch/data/example/repository/MarketplaceRepositoryIntegrationTests.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,24 @@
1313
import org.opensearch.spring.boot.autoconfigure.test.DataOpenSearchTest;
1414
import org.opensearch.testcontainers.OpensearchContainer;
1515
import org.springframework.beans.factory.annotation.Autowired;
16-
import org.springframework.boot.test.util.TestPropertyValues;
17-
import org.springframework.context.ApplicationContextInitializer;
18-
import org.springframework.context.ConfigurableApplicationContext;
16+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
1917
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
20-
import org.springframework.test.context.ContextConfiguration;
2118
import org.testcontainers.junit.jupiter.Container;
2219
import org.testcontainers.junit.jupiter.Testcontainers;
2320

2421
@Testcontainers(disabledWithoutDocker = true)
2522
@DataOpenSearchTest
2623
@EnableElasticsearchRepositories(basePackageClasses = MarketplaceRepository.class)
27-
@ContextConfiguration(initializers = MarketplaceRepositoryIntegrationTests.Initializer.class)
2824
@Tag("integration-test")
2925
public class MarketplaceRepositoryIntegrationTests {
3026
@Container
27+
@ServiceConnection
3128
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.19.1")
3229
.withStartupAttempts(5)
3330
.withStartupTimeout(Duration.ofMinutes(2));
3431

3532
@Test
3633
void testMarketplaceRepository(@Autowired MarketplaceRepository repository) {
37-
assertThat(repository.findAll()).hasSize(0);
38-
}
39-
40-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
41-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
42-
TestPropertyValues.of("opensearch.uris=" + opensearch.getHttpHostAddress())
43-
.applyTo(configurableApplicationContext.getEnvironment());
44-
}
34+
assertThat(repository.findAll()).isEmpty();
4535
}
4636
}

0 commit comments

Comments
 (0)