Skip to content

Commit d84422a

Browse files
dreis2211wilkinsona
authored andcommitted
Use @DynamicPropertySource for Neo4J and Redis data tests
See gh-20676
1 parent ac56db7 commit d84422a

File tree

6 files changed

+48
-96
lines changed

6 files changed

+48
-96
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestIntegrationTests.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424

2525
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2626
import org.springframework.beans.factory.annotation.Autowired;
27-
import org.springframework.boot.test.util.TestPropertyValues;
2827
import org.springframework.context.ApplicationContext;
29-
import org.springframework.context.ApplicationContextInitializer;
30-
import org.springframework.context.ConfigurableApplicationContext;
31-
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.DynamicPropertyRegistry;
29+
import org.springframework.test.context.DynamicPropertySource;
3230

3331
import static org.assertj.core.api.Assertions.assertThat;
3432
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -40,14 +38,18 @@
4038
* @author Stephane Nicoll
4139
* @author Michael Simons
4240
*/
43-
@ContextConfiguration(initializers = DataNeo4jTestIntegrationTests.Initializer.class)
4441
@DataNeo4jTest
4542
@Testcontainers(disabledWithoutDocker = true)
4643
class DataNeo4jTestIntegrationTests {
4744

4845
@Container
4946
static final Neo4jContainer<?> neo4j = new Neo4jContainer<>().withoutAuthentication();
5047

48+
@DynamicPropertySource
49+
static void neo4jProperties(DynamicPropertyRegistry registry) {
50+
registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl);
51+
}
52+
5153
@Autowired
5254
private Session session;
5355

@@ -73,14 +75,4 @@ void didNotInjectExampleService() {
7375
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
7476
}
7577

76-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
77-
78-
@Override
79-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
80-
TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl())
81-
.applyTo(configurableApplicationContext.getEnvironment());
82-
}
83-
84-
}
85-
8678
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestPropertiesIntegrationTests.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
import org.testcontainers.junit.jupiter.Testcontainers;
2323

2424
import org.springframework.beans.factory.annotation.Autowired;
25-
import org.springframework.boot.test.util.TestPropertyValues;
26-
import org.springframework.context.ApplicationContextInitializer;
27-
import org.springframework.context.ConfigurableApplicationContext;
2825
import org.springframework.core.env.Environment;
29-
import org.springframework.test.context.ContextConfiguration;
26+
import org.springframework.test.context.DynamicPropertyRegistry;
27+
import org.springframework.test.context.DynamicPropertySource;
3028

3129
import static org.assertj.core.api.Assertions.assertThat;
3230

@@ -37,13 +35,17 @@
3735
* @author Artsiom Yudovin
3836
*/
3937
@Testcontainers(disabledWithoutDocker = true)
40-
@ContextConfiguration(initializers = DataNeo4jTestPropertiesIntegrationTests.Initializer.class)
4138
@DataNeo4jTest(properties = "spring.profiles.active=test")
4239
class DataNeo4jTestPropertiesIntegrationTests {
4340

4441
@Container
4542
static final Neo4jContainer<?> neo4j = new Neo4jContainer<>().withoutAuthentication();
4643

44+
@DynamicPropertySource
45+
static void neo4jProperties(DynamicPropertyRegistry registry) {
46+
registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl);
47+
}
48+
4749
@Autowired
4850
private Environment environment;
4951

@@ -52,14 +54,4 @@ void environmentWithNewProfile() {
5254
assertThat(this.environment.getActiveProfiles()).containsExactly("test");
5355
}
5456

55-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
56-
57-
@Override
58-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
59-
TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl())
60-
.applyTo(configurableApplicationContext.getEnvironment());
61-
}
62-
63-
}
64-
6557
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestWithIncludeFilterIntegrationTests.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
import org.testcontainers.junit.jupiter.Testcontainers;
2323

2424
import org.springframework.beans.factory.annotation.Autowired;
25-
import org.springframework.boot.test.util.TestPropertyValues;
26-
import org.springframework.context.ApplicationContextInitializer;
27-
import org.springframework.context.ConfigurableApplicationContext;
2825
import org.springframework.context.annotation.ComponentScan.Filter;
2926
import org.springframework.stereotype.Service;
30-
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.test.context.DynamicPropertyRegistry;
28+
import org.springframework.test.context.DynamicPropertySource;
3129

3230
import static org.assertj.core.api.Assertions.assertThat;
3331

@@ -38,13 +36,17 @@
3836
* @author Michael Simons
3937
*/
4038
@Testcontainers(disabledWithoutDocker = true)
41-
@ContextConfiguration(initializers = DataNeo4jTestWithIncludeFilterIntegrationTests.Initializer.class)
4239
@DataNeo4jTest(includeFilters = @Filter(Service.class))
4340
class DataNeo4jTestWithIncludeFilterIntegrationTests {
4441

4542
@Container
4643
static final Neo4jContainer<?> neo4j = new Neo4jContainer<>().withoutAuthentication();
4744

45+
@DynamicPropertySource
46+
static void neo4jProperties(DynamicPropertyRegistry registry) {
47+
registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl);
48+
}
49+
4850
@Autowired
4951
private ExampleService service;
5052

@@ -53,14 +55,4 @@ void testService() {
5355
assertThat(this.service.hasNode(ExampleGraph.class)).isFalse();
5456
}
5557

56-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
57-
58-
@Override
59-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
60-
TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl())
61-
.applyTo(configurableApplicationContext.getEnvironment());
62-
}
63-
64-
}
65-
6658
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,14 +25,12 @@
2525

2626
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2727
import org.springframework.beans.factory.annotation.Autowired;
28-
import org.springframework.boot.test.util.TestPropertyValues;
2928
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
3029
import org.springframework.context.ApplicationContext;
31-
import org.springframework.context.ApplicationContextInitializer;
32-
import org.springframework.context.ConfigurableApplicationContext;
3330
import org.springframework.data.redis.connection.RedisConnection;
3431
import org.springframework.data.redis.core.RedisOperations;
35-
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.DynamicPropertyRegistry;
33+
import org.springframework.test.context.DynamicPropertySource;
3634

3735
import static org.assertj.core.api.Assertions.assertThat;
3836
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -43,12 +41,18 @@
4341
* @author Jayaram Pradhan
4442
*/
4543
@Testcontainers(disabledWithoutDocker = true)
46-
@ContextConfiguration(initializers = DataRedisTestIntegrationTests.Initializer.class)
4744
@DataRedisTest
4845
class DataRedisTestIntegrationTests {
4946

47+
private static final Charset CHARSET = StandardCharsets.UTF_8;
48+
5049
@Container
51-
public static RedisContainer redis = new RedisContainer();
50+
static RedisContainer redis = new RedisContainer();
51+
52+
@DynamicPropertySource
53+
static void redisProperties(DynamicPropertyRegistry registry) {
54+
registry.add("spring.redis.port", redis::getFirstMappedPort);
55+
}
5256

5357
@Autowired
5458
private RedisOperations<Object, Object> operations;
@@ -59,8 +63,6 @@ class DataRedisTestIntegrationTests {
5963
@Autowired
6064
private ApplicationContext applicationContext;
6165

62-
private static final Charset CHARSET = StandardCharsets.UTF_8;
63-
6466
@Test
6567
void testRepository() {
6668
PersonHash personHash = new PersonHash();
@@ -79,14 +81,4 @@ void didNotInjectExampleService() {
7981
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
8082
}
8183

82-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
83-
84-
@Override
85-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
86-
TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort())
87-
.applyTo(configurableApplicationContext.getEnvironment());
88-
}
89-
90-
}
91-
9284
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestPropertiesIntegrationTests.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,12 +21,10 @@
2121
import org.testcontainers.junit.jupiter.Testcontainers;
2222

2323
import org.springframework.beans.factory.annotation.Autowired;
24-
import org.springframework.boot.test.util.TestPropertyValues;
2524
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
26-
import org.springframework.context.ApplicationContextInitializer;
27-
import org.springframework.context.ConfigurableApplicationContext;
2825
import org.springframework.core.env.Environment;
29-
import org.springframework.test.context.ContextConfiguration;
26+
import org.springframework.test.context.DynamicPropertyRegistry;
27+
import org.springframework.test.context.DynamicPropertySource;
3028

3129
import static org.assertj.core.api.Assertions.assertThat;
3230

@@ -37,13 +35,17 @@
3735
* @author Artsiom Yudovin
3836
*/
3937
@Testcontainers(disabledWithoutDocker = true)
40-
@ContextConfiguration(initializers = DataRedisTestPropertiesIntegrationTests.Initializer.class)
4138
@DataRedisTest(properties = "spring.profiles.active=test")
4239
class DataRedisTestPropertiesIntegrationTests {
4340

4441
@Container
4542
static final RedisContainer redis = new RedisContainer();
4643

44+
@DynamicPropertySource
45+
static void redisProperties(DynamicPropertyRegistry registry) {
46+
registry.add("spring.redis.port", redis::getFirstMappedPort);
47+
}
48+
4749
@Autowired
4850
private Environment environment;
4951

@@ -52,14 +54,4 @@ void environmentWithNewProfile() {
5254
assertThat(this.environment.getActiveProfiles()).containsExactly("test");
5355
}
5456

55-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
56-
57-
@Override
58-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
59-
TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort())
60-
.applyTo(configurableApplicationContext.getEnvironment());
61-
}
62-
63-
}
64-
6557
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,13 +21,11 @@
2121
import org.testcontainers.junit.jupiter.Testcontainers;
2222

2323
import org.springframework.beans.factory.annotation.Autowired;
24-
import org.springframework.boot.test.util.TestPropertyValues;
2524
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
26-
import org.springframework.context.ApplicationContextInitializer;
27-
import org.springframework.context.ConfigurableApplicationContext;
2825
import org.springframework.context.annotation.ComponentScan.Filter;
2926
import org.springframework.stereotype.Service;
30-
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.test.context.DynamicPropertyRegistry;
28+
import org.springframework.test.context.DynamicPropertySource;
3129

3230
import static org.assertj.core.api.Assertions.assertThat;
3331

@@ -37,13 +35,17 @@
3735
* @author Jayaram Pradhan
3836
*/
3937
@Testcontainers(disabledWithoutDocker = true)
40-
@ContextConfiguration(initializers = DataRedisTestWithIncludeFilterIntegrationTests.Initializer.class)
4138
@DataRedisTest(includeFilters = @Filter(Service.class))
4239
class DataRedisTestWithIncludeFilterIntegrationTests {
4340

4441
@Container
4542
static final RedisContainer redis = new RedisContainer();
4643

44+
@DynamicPropertySource
45+
static void redisProperties(DynamicPropertyRegistry registry) {
46+
registry.add("spring.redis.port", redis::getFirstMappedPort);
47+
}
48+
4749
@Autowired
4850
private ExampleRepository exampleRepository;
4951

@@ -59,14 +61,4 @@ void testService() {
5961
assertThat(this.service.hasRecord(savedEntity)).isTrue();
6062
}
6163

62-
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
63-
64-
@Override
65-
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
66-
TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort())
67-
.applyTo(configurableApplicationContext.getEnvironment());
68-
}
69-
70-
}
71-
7264
}

0 commit comments

Comments
 (0)