Use TestContainers util from spring-boot-test-support

See gh-10516
pull/11498/merge
Madhura Bhave 7 years ago
parent 996b3ef7f2
commit 9a6c339243

@ -16,25 +16,20 @@
package org.springframework.boot.autoconfigure.data.cassandra;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.rnorth.ducttape.TimeoutException;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.wait.HostPortWaitStrategy;
import org.testcontainers.containers.GenericContainer;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.testcontainers.DockerTestContainer;
import org.springframework.boot.testsupport.testcontainers.TestContainers;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
@ -50,13 +45,19 @@ import static org.assertj.core.api.Assertions.assertThat;
public class CassandraDataAutoConfigurationIntegrationTests {
@ClassRule
public static DockerTestContainer<FixedHostPortGenericContainer<?>> cassandra = new DockerTestContainer<>(
() -> new FixedHostPortGenericContainer<>("cassandra:latest")
.withFixedExposedPort(9042, 9042)
.waitingFor(new ConnectionVerifyingWaitStrategy()));
public static DockerTestContainer<GenericContainer<?>> cassandra = new DockerTestContainer<>(
TestContainers::cassandra);
private AnnotationConfigApplicationContext context;
@Before
public void setUp() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues
.of("spring.data.cassandra.port=" + cassandra.getMappedPort(9042))
.applyTo(this.context.getEnvironment());
}
@After
public void close() {
if (this.context != null) {
@ -66,7 +67,6 @@ public class CassandraDataAutoConfigurationIntegrationTests {
@Test
public void hasDefaultSchemaActionSet() {
this.context = new AnnotationConfigApplicationContext();
String cityPackage = City.class.getPackage().getName();
AutoConfigurationPackages.register(this.context, cityPackage);
this.context.register(CassandraAutoConfiguration.class,
@ -81,7 +81,6 @@ public class CassandraDataAutoConfigurationIntegrationTests {
@Test
public void hasRecreateSchemaActionSet() {
createTestKeyspaceIfNotExists();
this.context = new AnnotationConfigApplicationContext();
String cityPackage = City.class.getPackage().getName();
AutoConfigurationPackages.register(this.context, cityPackage);
TestPropertyValues
@ -97,41 +96,12 @@ public class CassandraDataAutoConfigurationIntegrationTests {
}
private void createTestKeyspaceIfNotExists() {
Cluster cluster = Cluster.builder().addContactPoint("localhost").build();
Cluster cluster = Cluster.builder().withPort(cassandra.getMappedPort(9042))
.addContactPoint("localhost").build();
try (Session session = cluster.connect()) {
session.execute("CREATE KEYSPACE IF NOT EXISTS boot_test"
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
}
}
static class ConnectionVerifyingWaitStrategy extends HostPortWaitStrategy {
@Override
protected void waitUntilReady() {
super.waitUntilReady();
try {
Unreliables.retryUntilTrue((int) this.startupTimeout.getSeconds(),
TimeUnit.SECONDS, checkConnection());
}
catch (TimeoutException ex) {
throw new IllegalStateException(ex);
}
}
private Callable<Boolean> checkConnection() {
return () -> {
try (Cluster cluster = Cluster.builder().addContactPoint("localhost")
.build()) {
cluster.connect();
return true;
}
catch (NoHostAvailableException ex) {
return false;
}
};
}
}
}

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.data.redis;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
@ -27,6 +28,7 @@ import org.springframework.boot.autoconfigure.data.alt.redis.CityRedisRepository
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.boot.autoconfigure.data.redis.city.City;
import org.springframework.boot.autoconfigure.data.redis.city.CityRepository;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.testcontainers.DockerTestContainer;
import org.springframework.boot.testsupport.testcontainers.TestContainers;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -44,10 +46,17 @@ public class RedisRepositoriesAutoConfigurationTests {
@ClassRule
public static DockerTestContainer<GenericContainer<?>> redis = new DockerTestContainer<>(
() -> TestContainers.redis());
TestContainers::redis);
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Before
public void setUp() {
TestPropertyValues
.of("spring.redis.port=" + redis.getMappedPort(6379))
.applyTo(this.context.getEnvironment());
}
@After
public void close() {
this.context.close();

@ -16,9 +16,7 @@
package org.springframework.boot.autoconfigure.session;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -28,7 +26,6 @@ import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.DockerTestContainer;
import org.springframework.session.data.mongo.ReactiveMongoOperationsSessionRepository;
import org.springframework.session.data.redis.ReactiveRedisOperationsSessionRepository;
import org.springframework.session.data.redis.RedisFlushMode;
@ -45,11 +42,6 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ReactiveSessionAutoConfigurationRedisTests
extends AbstractSessionAutoConfigurationTests {
@ClassRule
public static DockerTestContainer<FixedHostPortGenericContainer<?>> redis = new DockerTestContainer<>(
() -> new FixedHostPortGenericContainer<>("redis:latest")
.withFixedExposedPort(6379, 6379));
protected final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SessionAutoConfiguration.class));

@ -49,7 +49,7 @@ public class SessionAutoConfigurationRedisTests
@ClassRule
public static DockerTestContainer<GenericContainer<?>> redis = new DockerTestContainer<>(
() -> TestContainers.redis());
TestContainers::redis);
protected final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SessionAutoConfiguration.class));

@ -16,26 +16,23 @@
package org.springframework.boot.test.autoconfigure.data.neo4j;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.rnorth.ducttape.TimeoutException;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.wait.HostPortWaitStrategy;
import org.testcontainers.containers.GenericContainer;
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.DockerTestContainer;
import org.springframework.boot.testsupport.testcontainers.TestContainers;
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.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@ -47,15 +44,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(initializers = DataNeo4jTestIntegrationTests.Initializer.class)
@DataNeo4jTest
public class DataNeo4jTestIntegrationTests {
@ClassRule
public static DockerTestContainer<FixedHostPortGenericContainer<?>> neo4j = new DockerTestContainer<>(
() -> new FixedHostPortGenericContainer<>("neo4j:latest")
.withFixedExposedPort(7687, 7687)
.waitingFor(new ConnectionVerifyingWaitStrategy())
.withEnv("NEO4J_AUTH", "none"));
public static DockerTestContainer<GenericContainer<?>> neo4j = new DockerTestContainer<>(
TestContainers::neo4j);
@Rule
public ExpectedException thrown = ExpectedException.none();
@ -85,35 +80,17 @@ public class DataNeo4jTestIntegrationTests {
this.applicationContext.getBean(ExampleService.class);
}
static class ConnectionVerifyingWaitStrategy extends HostPortWaitStrategy {
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
protected void waitUntilReady() {
super.waitUntilReady();
Configuration configuration = new Configuration.Builder()
.uri("bolt://localhost:7687").build();
SessionFactory sessionFactory = new SessionFactory(configuration,
"org.springframework.boot.test.autoconfigure.data.neo4j");
try {
Unreliables.retryUntilTrue((int) this.startupTimeout.getSeconds(),
TimeUnit.SECONDS, checkConnection(sessionFactory));
}
catch (TimeoutException e) {
throw new IllegalStateException();
}
public void initialize(
ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues
.of("spring.data.neo4j.uri=bolt://localhost:" + neo4j.getMappedPort(7687))
.applyTo(configurableApplicationContext.getEnvironment());
}
private Callable<Boolean> checkConnection(SessionFactory sessionFactory) {
return () -> {
try {
sessionFactory.openSession().beginTransaction().close();
return true;
}
catch (Exception ex) {
return false;
}
};
}
}
}

@ -19,12 +19,17 @@ package org.springframework.boot.test.autoconfigure.data.neo4j;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.GenericContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.testcontainers.DockerTestContainer;
import org.springframework.boot.testsupport.testcontainers.TestContainers;
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.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@ -35,16 +40,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Eddú Meléndez
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(initializers = DataNeo4jTestWithIncludeFilterIntegrationTests.Initializer.class)
@DataNeo4jTest(includeFilters = @Filter(Service.class))
public class DataNeo4jTestWithIncludeFilterIntegrationTests {
@ClassRule
public static DockerTestContainer<FixedHostPortGenericContainer<?>> neo4j = new DockerTestContainer<>(
() -> new FixedHostPortGenericContainer<>("neo4j:latest")
.withFixedExposedPort(7687, 7687)
.waitingFor(
new DataNeo4jTestIntegrationTests.ConnectionVerifyingWaitStrategy())
.withEnv("NEO4J_AUTH", "none"));
public static DockerTestContainer<GenericContainer<?>> neo4j = new DockerTestContainer<>(
TestContainers::neo4j);
@Autowired
private ExampleService service;
@ -54,4 +56,17 @@ public 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=bolt://localhost:" + neo4j.getMappedPort(7687))
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}

@ -24,14 +24,19 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.GenericContainer;
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.DockerTestContainer;
import org.springframework.boot.testsupport.testcontainers.TestContainers;
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.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@ -42,13 +47,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Jayaram Pradhan
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(initializers = DataRedisTestIntegrationTests.Initializer.class)
@DataRedisTest
public class DataRedisTestIntegrationTests {
@ClassRule
public static DockerTestContainer<FixedHostPortGenericContainer<?>> redis = new DockerTestContainer<>(
() -> new FixedHostPortGenericContainer<>("redis:latest")
.withFixedExposedPort(6379, 6379));
public static DockerTestContainer<GenericContainer<?>> redis = new DockerTestContainer<>(
TestContainers::redis);
@Rule
public ExpectedException thrown = ExpectedException.none();
@ -82,4 +87,17 @@ public class DataRedisTestIntegrationTests {
this.applicationContext.getBean(ExampleService.class);
}
static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(
ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues
.of("spring.redis.port=" + redis.getMappedPort(6379))
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}

@ -19,12 +19,17 @@ package org.springframework.boot.test.autoconfigure.data.redis;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.GenericContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.testcontainers.DockerTestContainer;
import org.springframework.boot.testsupport.testcontainers.TestContainers;
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.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@ -35,13 +40,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Jayaram Pradhan
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(initializers = DataRedisTestWithIncludeFilterIntegrationTests.Initializer.class)
@DataRedisTest(includeFilters = @Filter(Service.class))
public class DataRedisTestWithIncludeFilterIntegrationTests {
@ClassRule
public static DockerTestContainer<FixedHostPortGenericContainer<?>> redis = new DockerTestContainer<>(
() -> new FixedHostPortGenericContainer<>("redis:latest")
.withFixedExposedPort(6379, 6379));
public static DockerTestContainer<GenericContainer<?>> redis = new DockerTestContainer<>(
TestContainers::redis);
@Autowired
private ExampleRepository exampleRepository;
@ -58,4 +63,17 @@ public 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.getMappedPort(6379))
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}

Loading…
Cancel
Save