Commit 5cd910c7 authored by Andy Wilkinson's avatar Andy Wilkinson

Replace Embedded Mongo with Testcontainers' MongoDB support

Closes gh-23090
parent 84a12c84
...@@ -249,11 +249,6 @@ ...@@ -249,11 +249,6 @@
<artifactId>unboundid-ldapsdk</artifactId> <artifactId>unboundid-ldapsdk</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>io.lettuce</groupId> <groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId> <artifactId>lettuce-core</artifactId>
...@@ -356,6 +351,11 @@ ...@@ -356,6 +351,11 @@
<artifactId>junit-jupiter</artifactId> <artifactId>junit-jupiter</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.testcontainers</groupId> <groupId>org.testcontainers</groupId>
<artifactId>neo4j</artifactId> <artifactId>neo4j</artifactId>
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,12 +16,21 @@ ...@@ -16,12 +16,21 @@
package org.springframework.boot.test.autoconfigure.data.mongo; package org.springframework.boot.test.autoconfigure.data.mongo;
import java.time.Duration;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
...@@ -32,8 +41,14 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; ...@@ -32,8 +41,14 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Michael Simons * @author Michael Simons
*/ */
@DataMongoTest @DataMongoTest
@Testcontainers(disabledWithoutDocker = true)
@ContextConfiguration(initializers = DataMongoTestIntegrationTests.Initializer.class)
class DataMongoTestIntegrationTests { class DataMongoTestIntegrationTests {
@Container
static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(5));
@Autowired @Autowired
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
...@@ -58,4 +73,14 @@ class DataMongoTestIntegrationTests { ...@@ -58,4 +73,14 @@ class DataMongoTestIntegrationTests {
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class)); .isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
} }
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,9 +19,16 @@ package org.springframework.boot.test.autoconfigure.data.mongo; ...@@ -19,9 +19,16 @@ package org.springframework.boot.test.autoconfigure.data.mongo;
import java.time.Duration; import java.time.Duration;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired; 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.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -31,8 +38,14 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -31,8 +38,14 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@DataMongoTest @DataMongoTest
@Testcontainers(disabledWithoutDocker = true)
@ContextConfiguration(initializers = DataMongoTestReactiveIntegrationTests.Initializer.class)
class DataMongoTestReactiveIntegrationTests { class DataMongoTestReactiveIntegrationTests {
@Container
static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(5));
@Autowired @Autowired
private ReactiveMongoTemplate mongoTemplate; private ReactiveMongoTemplate mongoTemplate;
...@@ -48,4 +61,14 @@ class DataMongoTestReactiveIntegrationTests { ...@@ -48,4 +61,14 @@ class DataMongoTestReactiveIntegrationTests {
assertThat(this.mongoTemplate.collectionExists("exampleDocuments").block(Duration.ofSeconds(30))).isTrue(); assertThat(this.mongoTemplate.collectionExists("exampleDocuments").block(Duration.ofSeconds(30))).isTrue();
} }
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,11 +16,20 @@ ...@@ -16,11 +16,20 @@
package org.springframework.boot.test.autoconfigure.data.mongo; package org.springframework.boot.test.autoconfigure.data.mongo;
import java.time.Duration;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired; 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.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -30,8 +39,14 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -30,8 +39,14 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Michael Simons * @author Michael Simons
*/ */
@DataMongoTest(includeFilters = @Filter(Service.class)) @DataMongoTest(includeFilters = @Filter(Service.class))
@Testcontainers(disabledWithoutDocker = true)
@ContextConfiguration(initializers = DataMongoTestWithIncludeFilterIntegrationTests.Initializer.class)
class DataMongoTestWithIncludeFilterIntegrationTests { class DataMongoTestWithIncludeFilterIntegrationTests {
@Container
static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(5));
@Autowired @Autowired
private ExampleService service; private ExampleService service;
...@@ -40,4 +55,14 @@ class DataMongoTestWithIncludeFilterIntegrationTests { ...@@ -40,4 +55,14 @@ class DataMongoTestWithIncludeFilterIntegrationTests {
assertThat(this.service.hasCollection("foobar")).isFalse(); assertThat(this.service.hasCollection("foobar")).isFalse();
} }
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment