Use DynamicPropertySource in MongoDB tests

See gh-23127
pull/23585/head
dreis2211 4 years ago committed by Andy Wilkinson
parent 3df58b2d8d
commit 51c9dee899

@ -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<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}

@ -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<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}

Loading…
Cancel
Save