From 1504e3043d8e134f90143239995ff141c86af7bc Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 9 May 2023 08:22:24 +0200 Subject: [PATCH] Document devtools when using testcontainers at dev time See gh-35200 --- .../spring-boot-docs/build.gradle | 1 + .../src/docs/asciidoc/features/testing.adoc | 14 ++++++-- .../devtools/MyContainersConfiguration.java | 34 +++++++++++++++++++ .../devtools/MyContainersConfiguration.kt | 32 +++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.kt diff --git a/spring-boot-project/spring-boot-docs/build.gradle b/spring-boot-project/spring-boot-docs/build.gradle index b2eac745a23..08acfd01e12 100644 --- a/spring-boot-project/spring-boot-docs/build.gradle +++ b/spring-boot-project/spring-boot-docs/build.gradle @@ -75,6 +75,7 @@ dependencies { implementation(project(path: ":spring-boot-project:spring-boot-test")) implementation(project(path: ":spring-boot-project:spring-boot-test-autoconfigure")) implementation(project(path: ":spring-boot-project:spring-boot-testcontainers")) + implementation(project(path: ":spring-boot-project:spring-boot-devtools")) implementation("ch.qos.logback:logback-classic") implementation("com.zaxxer:HikariCP") implementation("io.micrometer:micrometer-core") diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc index 2e9798283e2..76b1663def1 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc @@ -1048,8 +1048,7 @@ include::code:test/TestMyApplication[] You can now launch `TestMyApplication` as you would any regular Java `main` method application to start your application and the containers that it needs to run. - - +TIP: You can use the Maven goal `spring-boot:test-run` or the Gradle task `bootTestRun` to do this from the command line. [[features.testing.testcontainers.at-development-time.dynamic-properties]] ===== Contributing Dynamic Properties at Development Time @@ -1084,6 +1083,17 @@ TIP: You can use the `@ServiceConnection` annotation on `Container` fields to es You can also add <> to your declaration class. +[[features.testing.testcontainers.at-development-time.devtools]] +===== Using DevTools with Testcontainers at Development Time + +When using devtools, you can annotate beans and bean methods with `@RestartScope`. +Such beans won't be recreated when the devtools restart the application. +This is especially useful for Testcontainer `Container` beans, as they keep their state despite the application restart. + +include::code:MyContainersConfiguration[] + +WARNING: If you're using Gradle and want to use this feature, you need to change the configuration of the `spring-boot-devtools` dependency from `developmentOnly` to `testImplementation`. +With the default scope of `developmentOnly`, the `bootTestRun` task will not pick up changes in your code, as the devtools are not active. [[features.testing.utilities]] === Test Utilities diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.java new file mode 100644 index 00000000000..c005a9bace9 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2023 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.testing.testcontainers.atdevelopmenttime.devtools; + +import org.testcontainers.containers.MongoDBContainer; + +import org.springframework.boot.devtools.restart.RestartScope; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; + +@TestConfiguration(proxyBeanMethods = false) +public class MyContainersConfiguration { + + @Bean + @RestartScope + public MongoDBContainer monogDbContainer() { + return new MongoDBContainer("mongo:5.0"); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.kt new file mode 100644 index 00000000000..a5d2f59f48b --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2023 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.boot.docs.features.testing.testcontainers.atdevelopmenttime.devtools + +import org.springframework.boot.devtools.restart.RestartScope +import org.springframework.boot.test.context.TestConfiguration +import org.springframework.context.annotation.Bean +import org.testcontainers.containers.MongoDBContainer + +@TestConfiguration(proxyBeanMethods = false) +class MyContainersConfiguration { + + @Bean + @RestartScope + fun monogDbContainer(): MongoDBContainer { + return MongoDBContainer("mongo:5.0") + } + +}