From d84422a41cd6b142a02eaf1ac92162bdd0685072 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Wed, 25 Mar 2020 18:17:39 +0100 Subject: [PATCH] Use @DynamicPropertySource for Neo4J and Redis data tests See gh-20676 --- .../neo4j/DataNeo4jTestIntegrationTests.java | 22 +++++--------- ...taNeo4jTestPropertiesIntegrationTests.java | 22 +++++--------- ...TestWithIncludeFilterIntegrationTests.java | 22 +++++--------- .../redis/DataRedisTestIntegrationTests.java | 30 +++++++------------ ...taRedisTestPropertiesIntegrationTests.java | 24 +++++---------- ...TestWithIncludeFilterIntegrationTests.java | 24 +++++---------- 6 files changed, 48 insertions(+), 96 deletions(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestIntegrationTests.java index eba313f1ab..fba328b0eb 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestIntegrationTests.java @@ -24,11 +24,9 @@ import org.testcontainers.junit.jupiter.Testcontainers; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; -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; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -40,7 +38,6 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; * @author Stephane Nicoll * @author Michael Simons */ -@ContextConfiguration(initializers = DataNeo4jTestIntegrationTests.Initializer.class) @DataNeo4jTest @Testcontainers(disabledWithoutDocker = true) class DataNeo4jTestIntegrationTests { @@ -48,6 +45,11 @@ class DataNeo4jTestIntegrationTests { @Container static final Neo4jContainer neo4j = new Neo4jContainer<>().withoutAuthentication(); + @DynamicPropertySource + static void neo4jProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl); + } + @Autowired private Session session; @@ -73,14 +75,4 @@ class DataNeo4jTestIntegrationTests { .isThrownBy(() -> this.applicationContext.getBean(ExampleService.class)); } - static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl()) - .applyTo(configurableApplicationContext.getEnvironment()); - } - - } - } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestPropertiesIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestPropertiesIntegrationTests.java index 642c536f13..77acadf9a5 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestPropertiesIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestPropertiesIntegrationTests.java @@ -22,11 +22,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.core.env.Environment; -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; @@ -37,13 +35,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Artsiom Yudovin */ @Testcontainers(disabledWithoutDocker = true) -@ContextConfiguration(initializers = DataNeo4jTestPropertiesIntegrationTests.Initializer.class) @DataNeo4jTest(properties = "spring.profiles.active=test") class DataNeo4jTestPropertiesIntegrationTests { @Container static final Neo4jContainer neo4j = new Neo4jContainer<>().withoutAuthentication(); + @DynamicPropertySource + static void neo4jProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl); + } + @Autowired private Environment environment; @@ -52,14 +54,4 @@ class DataNeo4jTestPropertiesIntegrationTests { assertThat(this.environment.getActiveProfiles()).containsExactly("test"); } - static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl()) - .applyTo(configurableApplicationContext.getEnvironment()); - } - - } - } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestWithIncludeFilterIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestWithIncludeFilterIntegrationTests.java index e82fba702c..88ff5d3eb3 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestWithIncludeFilterIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestWithIncludeFilterIntegrationTests.java @@ -22,12 +22,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; @@ -38,13 +36,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Michael Simons */ @Testcontainers(disabledWithoutDocker = true) -@ContextConfiguration(initializers = DataNeo4jTestWithIncludeFilterIntegrationTests.Initializer.class) @DataNeo4jTest(includeFilters = @Filter(Service.class)) class DataNeo4jTestWithIncludeFilterIntegrationTests { @Container static final Neo4jContainer neo4j = new Neo4jContainer<>().withoutAuthentication(); + @DynamicPropertySource + static void neo4jProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl); + } + @Autowired private ExampleService service; @@ -53,14 +55,4 @@ class DataNeo4jTestWithIncludeFilterIntegrationTests { assertThat(this.service.hasNode(ExampleGraph.class)).isFalse(); } - static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of("spring.data.neo4j.uri=" + neo4j.getBoltUrl()) - .applyTo(configurableApplicationContext.getEnvironment()); - } - - } - } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java index bd333ba47c..fc26c3ca7a 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,14 +25,12 @@ import org.testcontainers.junit.jupiter.Testcontainers; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.testsupport.testcontainers.RedisContainer; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisOperations; -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; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -43,12 +41,18 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; * @author Jayaram Pradhan */ @Testcontainers(disabledWithoutDocker = true) -@ContextConfiguration(initializers = DataRedisTestIntegrationTests.Initializer.class) @DataRedisTest class DataRedisTestIntegrationTests { + private static final Charset CHARSET = StandardCharsets.UTF_8; + @Container - public static RedisContainer redis = new RedisContainer(); + static RedisContainer redis = new RedisContainer(); + + @DynamicPropertySource + static void redisProperties(DynamicPropertyRegistry registry) { + registry.add("spring.redis.port", redis::getFirstMappedPort); + } @Autowired private RedisOperations operations; @@ -59,8 +63,6 @@ class DataRedisTestIntegrationTests { @Autowired private ApplicationContext applicationContext; - private static final Charset CHARSET = StandardCharsets.UTF_8; - @Test void testRepository() { PersonHash personHash = new PersonHash(); @@ -79,14 +81,4 @@ class DataRedisTestIntegrationTests { .isThrownBy(() -> this.applicationContext.getBean(ExampleService.class)); } - static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort()) - .applyTo(configurableApplicationContext.getEnvironment()); - } - - } - } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestPropertiesIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestPropertiesIntegrationTests.java index b598d652d7..0724fa06a4 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestPropertiesIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestPropertiesIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,12 +21,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.boot.testsupport.testcontainers.RedisContainer; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.Environment; -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; @@ -37,13 +35,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Artsiom Yudovin */ @Testcontainers(disabledWithoutDocker = true) -@ContextConfiguration(initializers = DataRedisTestPropertiesIntegrationTests.Initializer.class) @DataRedisTest(properties = "spring.profiles.active=test") class DataRedisTestPropertiesIntegrationTests { @Container static final RedisContainer redis = new RedisContainer(); + @DynamicPropertySource + static void redisProperties(DynamicPropertyRegistry registry) { + registry.add("spring.redis.port", redis::getFirstMappedPort); + } + @Autowired private Environment environment; @@ -52,14 +54,4 @@ class DataRedisTestPropertiesIntegrationTests { assertThat(this.environment.getActiveProfiles()).containsExactly("test"); } - static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort()) - .applyTo(configurableApplicationContext.getEnvironment()); - } - - } - } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java index 3059fbb769..362f6dffe1 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestWithIncludeFilterIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,13 +21,11 @@ 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.boot.testsupport.testcontainers.RedisContainer; -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; @@ -37,13 +35,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Jayaram Pradhan */ @Testcontainers(disabledWithoutDocker = true) -@ContextConfiguration(initializers = DataRedisTestWithIncludeFilterIntegrationTests.Initializer.class) @DataRedisTest(includeFilters = @Filter(Service.class)) class DataRedisTestWithIncludeFilterIntegrationTests { @Container static final RedisContainer redis = new RedisContainer(); + @DynamicPropertySource + static void redisProperties(DynamicPropertyRegistry registry) { + registry.add("spring.redis.port", redis::getFirstMappedPort); + } + @Autowired private ExampleRepository exampleRepository; @@ -59,14 +61,4 @@ class DataRedisTestWithIncludeFilterIntegrationTests { assertThat(this.service.hasRecord(savedEntity)).isTrue(); } - static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort()) - .applyTo(configurableApplicationContext.getEnvironment()); - } - - } - }