Use @DynamicPropertySource for Neo4J and Redis data tests

See gh-20676
This commit is contained in:
dreis2211
2020-03-25 18:17:39 +01:00
committed by Andy Wilkinson
parent ac56db703e
commit d84422a41c
6 changed files with 48 additions and 96 deletions

View File

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

View File

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

View File

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

View File

@@ -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<Object, Object> 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<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}

View File

@@ -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<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}

View File

@@ -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<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.redis.port=" + redis.getFirstMappedPort())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}