Browse Source

Document devtools when using testcontainers at dev time

See gh-35200
pull/35366/head
Moritz Halbritter 3 years ago
parent
commit
1504e3043d
  1. 1
      spring-boot-project/spring-boot-docs/build.gradle
  2. 14
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc
  3. 34
      spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.java
  4. 32
      spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.kt

1
spring-boot-project/spring-boot-docs/build.gradle

@ -75,6 +75,7 @@ dependencies { @@ -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")

14
spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc

@ -1048,8 +1048,7 @@ include::code:test/TestMyApplication[] @@ -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 @@ -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

34
spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.java

@ -0,0 +1,34 @@ @@ -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");
}
}

32
spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.kt

@ -0,0 +1,32 @@ @@ -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")
}
}
Loading…
Cancel
Save