Document devtools when using testcontainers at dev time

See gh-35200
This commit is contained in:
Moritz Halbritter
2023-05-09 08:22:24 +02:00
parent 21406e7567
commit 1504e3043d
4 changed files with 79 additions and 2 deletions

View File

@@ -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")

View File

@@ -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 <<features#features.testing.testcontainers.dynamic-properties,`@DynamicPropertySource` annotated methods>> 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

View File

@@ -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");
}
}

View File

@@ -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")
}
}