Skip to content

Commit 5cd910c

Browse files
committed
Replace Embedded Mongo with Testcontainers' MongoDB support
Closes gh-23090
1 parent 84a12c8 commit 5cd910c

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

spring-boot-project/spring-boot-test-autoconfigure/pom.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,6 @@
249249
<artifactId>unboundid-ldapsdk</artifactId>
250250
<scope>test</scope>
251251
</dependency>
252-
<dependency>
253-
<groupId>de.flapdoodle.embed</groupId>
254-
<artifactId>de.flapdoodle.embed.mongo</artifactId>
255-
<scope>test</scope>
256-
</dependency>
257252
<dependency>
258253
<groupId>io.lettuce</groupId>
259254
<artifactId>lettuce-core</artifactId>
@@ -356,6 +351,11 @@
356351
<artifactId>junit-jupiter</artifactId>
357352
<scope>test</scope>
358353
</dependency>
354+
<dependency>
355+
<groupId>org.testcontainers</groupId>
356+
<artifactId>mongodb</artifactId>
357+
<scope>test</scope>
358+
</dependency>
359359
<dependency>
360360
<groupId>org.testcontainers</groupId>
361361
<artifactId>neo4j</artifactId>

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java

+26-1
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.
@@ -16,12 +16,21 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.mongo;
1818

19+
import java.time.Duration;
20+
1921
import org.junit.jupiter.api.Test;
22+
import org.testcontainers.containers.MongoDBContainer;
23+
import org.testcontainers.junit.jupiter.Container;
24+
import org.testcontainers.junit.jupiter.Testcontainers;
2025

2126
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2227
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.util.TestPropertyValues;
2329
import org.springframework.context.ApplicationContext;
30+
import org.springframework.context.ApplicationContextInitializer;
31+
import org.springframework.context.ConfigurableApplicationContext;
2432
import org.springframework.data.mongodb.core.MongoTemplate;
33+
import org.springframework.test.context.ContextConfiguration;
2534

2635
import static org.assertj.core.api.Assertions.assertThat;
2736
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -32,8 +41,14 @@
3241
* @author Michael Simons
3342
*/
3443
@DataMongoTest
44+
@Testcontainers(disabledWithoutDocker = true)
45+
@ContextConfiguration(initializers = DataMongoTestIntegrationTests.Initializer.class)
3546
class DataMongoTestIntegrationTests {
3647

48+
@Container
49+
static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5)
50+
.withStartupTimeout(Duration.ofMinutes(5));
51+
3752
@Autowired
3853
private MongoTemplate mongoTemplate;
3954

@@ -58,4 +73,14 @@ void didNotInjectExampleService() {
5873
.isThrownBy(() -> this.applicationContext.getBean(ExampleService.class));
5974
}
6075

76+
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
77+
78+
@Override
79+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
80+
TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl())
81+
.applyTo(configurableApplicationContext.getEnvironment());
82+
}
83+
84+
}
85+
6186
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java

+24-1
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.
@@ -19,9 +19,16 @@
1919
import java.time.Duration;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.testcontainers.containers.MongoDBContainer;
23+
import org.testcontainers.junit.jupiter.Container;
24+
import org.testcontainers.junit.jupiter.Testcontainers;
2225

2326
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.util.TestPropertyValues;
28+
import org.springframework.context.ApplicationContextInitializer;
29+
import org.springframework.context.ConfigurableApplicationContext;
2430
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
31+
import org.springframework.test.context.ContextConfiguration;
2532

2633
import static org.assertj.core.api.Assertions.assertThat;
2734

@@ -31,8 +38,14 @@
3138
* @author Stephane Nicoll
3239
*/
3340
@DataMongoTest
41+
@Testcontainers(disabledWithoutDocker = true)
42+
@ContextConfiguration(initializers = DataMongoTestReactiveIntegrationTests.Initializer.class)
3443
class DataMongoTestReactiveIntegrationTests {
3544

45+
@Container
46+
static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5)
47+
.withStartupTimeout(Duration.ofMinutes(5));
48+
3649
@Autowired
3750
private ReactiveMongoTemplate mongoTemplate;
3851

@@ -48,4 +61,14 @@ void testRepository() {
4861
assertThat(this.mongoTemplate.collectionExists("exampleDocuments").block(Duration.ofSeconds(30))).isTrue();
4962
}
5063

64+
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
65+
66+
@Override
67+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
68+
TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl())
69+
.applyTo(configurableApplicationContext.getEnvironment());
70+
}
71+
72+
}
73+
5174
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java

+26-1
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.
@@ -16,11 +16,20 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.mongo;
1818

19+
import java.time.Duration;
20+
1921
import org.junit.jupiter.api.Test;
22+
import org.testcontainers.containers.MongoDBContainer;
23+
import org.testcontainers.junit.jupiter.Container;
24+
import org.testcontainers.junit.jupiter.Testcontainers;
2025

2126
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.test.util.TestPropertyValues;
28+
import org.springframework.context.ApplicationContextInitializer;
29+
import org.springframework.context.ConfigurableApplicationContext;
2230
import org.springframework.context.annotation.ComponentScan.Filter;
2331
import org.springframework.stereotype.Service;
32+
import org.springframework.test.context.ContextConfiguration;
2433

2534
import static org.assertj.core.api.Assertions.assertThat;
2635

@@ -30,8 +39,14 @@
3039
* @author Michael Simons
3140
*/
3241
@DataMongoTest(includeFilters = @Filter(Service.class))
42+
@Testcontainers(disabledWithoutDocker = true)
43+
@ContextConfiguration(initializers = DataMongoTestWithIncludeFilterIntegrationTests.Initializer.class)
3344
class DataMongoTestWithIncludeFilterIntegrationTests {
3445

46+
@Container
47+
static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5)
48+
.withStartupTimeout(Duration.ofMinutes(5));
49+
3550
@Autowired
3651
private ExampleService service;
3752

@@ -40,4 +55,14 @@ void testService() {
4055
assertThat(this.service.hasCollection("foobar")).isFalse();
4156
}
4257

58+
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
59+
60+
@Override
61+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
62+
TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl())
63+
.applyTo(configurableApplicationContext.getEnvironment());
64+
}
65+
66+
}
67+
4368
}

0 commit comments

Comments
 (0)