Skip to content

Commit d051146

Browse files
committed
Use Testcontainers instead on Embedded Mongo
We've seen some problems, particularly on CI, where Embedded Mongo hangs while trying to start the Mongo process. To limit the problem, this commit replaces the use of Embedded Mongo with Testcontainers where we can, leaving Embedded Mongo in use only where we're specifically testing our Embedded Mongo support. Closes gh-28843
1 parent 5883b22 commit d051146

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/ReactiveSessionAutoConfigurationMongoTests.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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,18 +16,23 @@
1616

1717
package org.springframework.boot.autoconfigure.session;
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.boot.autoconfigure.AutoConfigurations;
2227
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
2328
import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration;
2429
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
2530
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
26-
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
2731
import org.springframework.boot.test.context.FilteredClassLoader;
2832
import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext;
2933
import org.springframework.boot.test.context.runner.ContextConsumer;
3034
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
35+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
3136
import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
3237
import org.springframework.session.data.redis.ReactiveRedisSessionRepository;
3338

@@ -38,34 +43,41 @@
3843
*
3944
* @author Andy Wilkinson
4045
*/
46+
@Testcontainers
4147
class ReactiveSessionAutoConfigurationMongoTests extends AbstractSessionAutoConfigurationTests {
4248

49+
@Container
50+
static final MongoDBContainer mongoDb = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(5)
51+
.withStartupTimeout(Duration.ofMinutes(5));
52+
4353
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
4454
.withConfiguration(AutoConfigurations.of(SessionAutoConfiguration.class));
4555

4656
@Test
4757
void defaultConfig() {
48-
this.contextRunner.withPropertyValues("spring.session.store-type=mongodb")
49-
.withConfiguration(AutoConfigurations.of(EmbeddedMongoAutoConfiguration.class,
50-
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
58+
this.contextRunner
59+
.withPropertyValues("spring.session.store-type=mongodb",
60+
"spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
61+
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
5162
MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class))
5263
.run(validateSpringSessionUsesMongo("sessions"));
5364
}
5465

5566
@Test
5667
void defaultConfigWithUniqueStoreImplementation() {
5768
this.contextRunner.withClassLoader(new FilteredClassLoader(ReactiveRedisSessionRepository.class))
58-
.withConfiguration(AutoConfigurations.of(EmbeddedMongoAutoConfiguration.class,
59-
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
69+
.withPropertyValues("spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
70+
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
6071
MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class))
6172
.run(validateSpringSessionUsesMongo("sessions"));
6273
}
6374

6475
@Test
6576
void defaultConfigWithCustomTimeout() {
66-
this.contextRunner.withPropertyValues("spring.session.store-type=mongodb", "spring.session.timeout=1m")
67-
.withConfiguration(AutoConfigurations.of(EmbeddedMongoAutoConfiguration.class,
68-
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
77+
this.contextRunner
78+
.withPropertyValues("spring.session.store-type=mongodb", "spring.session.timeout=1m",
79+
"spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
80+
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
6981
MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class))
7082
.run((context) -> {
7183
ReactiveMongoSessionRepository repository = validateSessionRepository(context,
@@ -77,10 +89,10 @@ void defaultConfigWithCustomTimeout() {
7789
@Test
7890
void mongoSessionStoreWithCustomizations() {
7991
this.contextRunner
80-
.withConfiguration(AutoConfigurations.of(EmbeddedMongoAutoConfiguration.class,
81-
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
92+
.withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
8293
MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class))
83-
.withPropertyValues("spring.session.store-type=mongodb", "spring.session.mongodb.collection-name=foo")
94+
.withPropertyValues("spring.session.store-type=mongodb", "spring.session.mongodb.collection-name=foo",
95+
"spring.data.mongodb.uri=" + mongoDb.getReplicaSetUrl())
8496
.run(validateSpringSessionUsesMongo("foo"));
8597
}
8698

0 commit comments

Comments
 (0)