From 5cd910c7cedf6bc0320fc4599095adb99036da47 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 28 Aug 2020 11:13:29 +0100 Subject: [PATCH] Replace Embedded Mongo with Testcontainers' MongoDB support Closes gh-23090 --- .../spring-boot-test-autoconfigure/pom.xml | 10 +++---- .../mongo/DataMongoTestIntegrationTests.java | 27 ++++++++++++++++++- ...DataMongoTestReactiveIntegrationTests.java | 25 ++++++++++++++++- ...TestWithIncludeFilterIntegrationTests.java | 27 ++++++++++++++++++- 4 files changed, 81 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/pom.xml b/spring-boot-project/spring-boot-test-autoconfigure/pom.xml index 1af6f9b299..90b8263304 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-project/spring-boot-test-autoconfigure/pom.xml @@ -249,11 +249,6 @@ unboundid-ldapsdk test - - de.flapdoodle.embed - de.flapdoodle.embed.mongo - test - io.lettuce lettuce-core @@ -356,6 +351,11 @@ junit-jupiter test + + org.testcontainers + mongodb + test + org.testcontainers neo4j diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java index dbf2152c7e..4aac327a29 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestIntegrationTests.java @@ -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. @@ -16,12 +16,21 @@ package org.springframework.boot.test.autoconfigure.data.mongo; +import java.time.Duration; + 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.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.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.assertThatExceptionOfType; @@ -32,8 +41,14 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; * @author Michael Simons */ @DataMongoTest +@Testcontainers(disabledWithoutDocker = true) +@ContextConfiguration(initializers = DataMongoTestIntegrationTests.Initializer.class) class DataMongoTestIntegrationTests { + @Container + static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(5)); + @Autowired private MongoTemplate mongoTemplate; @@ -58,4 +73,14 @@ class DataMongoTestIntegrationTests { .isThrownBy(() -> this.applicationContext.getBean(ExampleService.class)); } + static class Initializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl()) + .applyTo(configurableApplicationContext.getEnvironment()); + } + + } + } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java index 3b941f5c8b..b6b89249a5 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestReactiveIntegrationTests.java @@ -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. @@ -19,9 +19,16 @@ package org.springframework.boot.test.autoconfigure.data.mongo; import java.time.Duration; 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.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; +import org.springframework.test.context.ContextConfiguration; import static org.assertj.core.api.Assertions.assertThat; @@ -31,8 +38,14 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Stephane Nicoll */ @DataMongoTest +@Testcontainers(disabledWithoutDocker = true) +@ContextConfiguration(initializers = DataMongoTestReactiveIntegrationTests.Initializer.class) class DataMongoTestReactiveIntegrationTests { + @Container + static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(5)); + @Autowired private ReactiveMongoTemplate mongoTemplate; @@ -48,4 +61,14 @@ class DataMongoTestReactiveIntegrationTests { assertThat(this.mongoTemplate.collectionExists("exampleDocuments").block(Duration.ofSeconds(30))).isTrue(); } + static class Initializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl()) + .applyTo(configurableApplicationContext.getEnvironment()); + } + + } + } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java index 15d9d7e5b8..3d59910eab 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestWithIncludeFilterIntegrationTests.java @@ -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. @@ -16,11 +16,20 @@ package org.springframework.boot.test.autoconfigure.data.mongo; +import java.time.Duration; + 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.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 static org.assertj.core.api.Assertions.assertThat; @@ -30,8 +39,14 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Michael Simons */ @DataMongoTest(includeFilters = @Filter(Service.class)) +@Testcontainers(disabledWithoutDocker = true) +@ContextConfiguration(initializers = DataMongoTestWithIncludeFilterIntegrationTests.Initializer.class) class DataMongoTestWithIncludeFilterIntegrationTests { + @Container + static final MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10").withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(5)); + @Autowired private ExampleService service; @@ -40,4 +55,14 @@ class DataMongoTestWithIncludeFilterIntegrationTests { assertThat(this.service.hasCollection("foobar")).isFalse(); } + static class Initializer implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues.of("spring.data.mongodb.uri=" + mongoDB.getReplicaSetUrl()) + .applyTo(configurableApplicationContext.getEnvironment()); + } + + } + }