Skip to content

Commit e35bf8f

Browse files
committed
Merge pull request #20676 from dreis2211
* gh-20676: Polish "Use @DynamicPropertySource for Neo4J and Redis data tests" Use @DynamicPropertySource for Neo4J and Redis data tests Closes gh-20676
2 parents ac56db7 + ce95fd6 commit e35bf8f

File tree

6 files changed

+50
-98
lines changed

6 files changed

+50
-98
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestIntegrationTests.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.
@@ -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,7 +38,6 @@
4038
* @author Stephane Nicoll
4139
* @author Michael Simons
4240
*/
43-
@ContextConfiguration(initializers = DataNeo4jTestIntegrationTests.Initializer.class)
4441
@DataNeo4jTest
4542
@Testcontainers(disabledWithoutDocker = true)
4643
class DataNeo4jTestIntegrationTests {
@@ -57,6 +54,11 @@ class DataNeo4jTestIntegrationTests {
5754
@Autowired
5855
private ApplicationContext applicationContext;
5956

57+
@DynamicPropertySource
58+
static void neo4jProperties(DynamicPropertyRegistry registry) {
59+
registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl);
60+
}
61+
6062
@Test
6163
void testRepository() {
6264
ExampleGraph exampleGraph = new ExampleGraph();
@@ -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: 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.
@@ -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,7 +35,6 @@
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

@@ -47,19 +44,14 @@ class DataNeo4jTestPropertiesIntegrationTests {
4744
@Autowired
4845
private Environment environment;
4946

47+
@DynamicPropertySource
48+
static void neo4jProperties(DynamicPropertyRegistry registry) {
49+
registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl);
50+
}
51+
5052
@Test
5153
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: 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.
@@ -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,7 +36,6 @@
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

@@ -48,19 +45,14 @@ class DataNeo4jTestWithIncludeFilterIntegrationTests {
4845
@Autowired
4946
private ExampleService service;
5047

48+
@DynamicPropertySource
49+
static void neo4jProperties(DynamicPropertyRegistry registry) {
50+
registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl);
51+
}
52+
5153
@Test
5254
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: 10 additions & 18 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,13 @@
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();
5251

5352
@Autowired
5453
private RedisOperations<Object, Object> operations;
@@ -59,7 +58,10 @@ class DataRedisTestIntegrationTests {
5958
@Autowired
6059
private ApplicationContext applicationContext;
6160

62-
private static final Charset CHARSET = StandardCharsets.UTF_8;
61+
@DynamicPropertySource
62+
static void redisProperties(DynamicPropertyRegistry registry) {
63+
registry.add("spring.redis.port", redis::getFirstMappedPort);
64+
}
6365

6466
@Test
6567
void testRepository() {
@@ -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,7 +35,6 @@
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

@@ -47,19 +44,14 @@ class DataRedisTestPropertiesIntegrationTests {
4744
@Autowired
4845
private Environment environment;
4946

47+
@DynamicPropertySource
48+
static void redisProperties(DynamicPropertyRegistry registry) {
49+
registry.add("spring.redis.port", redis::getFirstMappedPort);
50+
}
51+
5052
@Test
5153
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,7 +35,6 @@
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

@@ -50,6 +47,11 @@ class DataRedisTestWithIncludeFilterIntegrationTests {
5047
@Autowired
5148
private ExampleService service;
5249

50+
@DynamicPropertySource
51+
static void redisProperties(DynamicPropertyRegistry registry) {
52+
registry.add("spring.redis.port", redis::getFirstMappedPort);
53+
}
54+
5355
@Test
5456
void testService() {
5557
PersonHash personHash = new PersonHash();
@@ -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)