From 51c9dee89929df5dca3ec1cb29d73b6116108d90 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 28 Aug 2020 14:04:35 +0200 Subject: [PATCH] Use DynamicPropertySource in MongoDB tests See gh-23127 --- ...DataMongoTestReactiveIntegrationTests.java | 22 ++++++------------- ...TestWithIncludeFilterIntegrationTests.java | 22 ++++++------------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java index b6b89249a5..e2b948c86f 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java @@ -24,11 +24,9 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import static org.assertj.core.api.Assertions.assertThat; @@ -39,7 +37,6 @@ import static org.assertj.core.api.Assertions.assertThat; */ @DataMongoTest @Testcontainers(disabledWithoutDocker = true) -@ContextConfiguration(initializers = DataMongoTestReactiveIntegrationTests.Initializer.class) class DataMongoTestReactiveIntegrationTests { @Container @@ -52,6 +49,11 @@ class DataMongoTestReactiveIntegrationTests { @Autowired private ExampleReactiveRepository exampleRepository; + @DynamicPropertySource + static void mongoProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.mongodb.uri", mongoDB::getReplicaSetUrl); + } + @Test void testRepository() { ExampleDocument exampleDocument = new ExampleDocument(); @@ -61,14 +63,4 @@ class DataMongoTestReactiveIntegrationTests { assertThat(this.mongoTemplate.collectionExists("exampleDocuments").block(Duration.ofSeconds(30))).isTrue(); } - static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl()) - .applyTo(configurableApplicationContext.getEnvironment()); - } - - } - } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java index 3d59910eab..bf3826564f 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java @@ -24,12 +24,10 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.stereotype.Service; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import static org.assertj.core.api.Assertions.assertThat; @@ -40,7 +38,6 @@ import static org.assertj.core.api.Assertions.assertThat; */ @DataMongoTest(includeFilters = @Filter(Service.class)) @Testcontainers(disabledWithoutDocker = true) -@ContextConfiguration(initializers = DataMongoTestWithIncludeFilterIntegrationTests.Initializer.class) class DataMongoTestWithIncludeFilterIntegrationTests { @Container @@ -50,19 +47,14 @@ class DataMongoTestWithIncludeFilterIntegrationTests { @Autowired private ExampleService service; + @DynamicPropertySource + static void mongoProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.mongodb.uri", mongoDB::getReplicaSetUrl); + } + @Test void testService() { assertThat(this.service.hasCollection("foobar")).isFalse(); } - static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl()) - .applyTo(configurableApplicationContext.getEnvironment()); - } - - } - }