You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As well as <<features#features.testing.testcontainers, using Testcontainers for integration testing>>, it's also possible to use them at development time.
4
+
The next sections will provide more details about that.
5
+
6
+
7
+
8
+
[[features.testcontainers.at-development-time]]
9
+
=== Using Testcontainers at Development Time
10
+
This approach allows developers to quickly start containers for the services that the application depends on, removing the need to manually provision things like database servers.
11
+
Using Testcontainers in this way provides functionality similar to Docker Compose, except that your container configuration is in Java rather than YAML.
12
+
13
+
To use Testcontainers at development time you need to launch your application using your "`test`" classpath rather than "`main`".
14
+
This will allow you to access all declared test dependencies and give you a natural place to write your test configuration.
15
+
16
+
To create a test launchable version of your application you should create an "`Application`" class in the `src/test` directory.
17
+
For example, if your main application is in `src/main/java/com/example/MyApplication.java`, you should create `src/test/java/com/example/TestMyApplication.java`
18
+
19
+
The `TestMyApplication` class can use the `SpringApplication.from(...)` method to launch the real application:
20
+
21
+
include::code:launch/TestMyApplication[]
22
+
23
+
You'll also need to define the `Container` instances that you want to start along with your application.
24
+
To do this, you need to make sure that the `spring-boot-testcontainers` module has been added as a `test` dependency.
25
+
Once that has been done, you can create a `@TestConfiguration` class that declares `@Bean` methods for the containers you want to start.
26
+
27
+
You can also annotate your `@Bean` methods with `@ServiceConnection` in order to create `ConnectionDetails` beans.
28
+
See <<features#features.testing.testcontainers.service-connections, the service connections>> section for details of the supported technologies.
29
+
30
+
A typical Testcontainers configuration would look like this:
31
+
32
+
include::code:test/MyContainersConfiguration[]
33
+
34
+
NOTE: The lifecycle of `Container` beans is automatically managed by Spring Boot.
35
+
Containers will be started and stopped automatically.
36
+
37
+
Once you have defined your test configuration, you can use the `with(...)` method to attach it to your test launcher:
38
+
39
+
include::code:test/TestMyApplication[]
40
+
41
+
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.
42
+
43
+
TIP: You can use the Maven goal `spring-boot:test-run` or the Gradle task `bootTestRun` to do this from the command line.
==== Contributing Dynamic Properties at Development Time
49
+
If you want to contribute dynamic properties at development time from your `Container` `@Bean` methods, you can do so by injecting a `DynamicPropertyRegistry`.
50
+
This works in a similar way to the <<features#features.testing.testcontainers.dynamic-properties,`@DynamicPropertySource` annotation>> that you can use in your tests.
51
+
It allows you to add properties that will become available once your container has started.
52
+
53
+
A typical configuration would look like this:
54
+
55
+
include::code:MyContainersConfiguration[]
56
+
57
+
NOTE: Using a `@ServiceConnection` is recommended whenever possible, however, dynamic properties can be a useful fallback for technologies that don't yet have `@ServiceConnection` support.
A common pattern when using Testcontainers is to declare `Container` instances as static fields.
64
+
Often these fields are defined directly on the test class.
65
+
They can also be declared on a parent class or on an interface that the test implements.
66
+
67
+
For example, the following `MyContainers` interface declares `mongo` and `neo4j` containers:
68
+
69
+
include::code:MyContainers[]
70
+
71
+
If you already have containers defined in this way, or you just prefer this style, you can import these declaration classes rather than defining you containers as `@Bean` methods.
72
+
To do so, add the `@ImportTestcontainers` annotation to your test configuration class:
73
+
74
+
include::code:MyContainersConfiguration[]
75
+
76
+
TIP: You can use the `@ServiceConnection` annotation on `Container` fields to establish service connections.
77
+
You can also add <<features#features.testing.testcontainers.dynamic-properties,`@DynamicPropertySource` annotated methods>> to your declaration class.
==== Using DevTools with Testcontainers at Development Time
83
+
When using devtools, you can annotate beans and bean methods with `@RestartScope`.
84
+
Such beans won't be recreated when the devtools restart the application.
85
+
This is especially useful for Testcontainer `Container` beans, as they keep their state despite the application restart.
86
+
87
+
include::code:MyContainersConfiguration[]
88
+
89
+
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`.
90
+
With the default scope of `developmentOnly`, the `bootTestRun` task will not pick up changes in your code, as the devtools are not active.
As well as using Testcontainers for integration testing, it's also possible to use them at development time.
1042
-
This approach allows developers to quickly start containers for the services that the application depends on, removing the need to manually provision things like database servers.
1043
-
Using Testcontainers in this way provides functionality similar to Docker Compose, except that your container configuration is in Java rather than YAML.
1044
-
1045
-
To use Testcontainers at development time you need to launch your application using your "`test`" classpath rather than "`main`".
1046
-
This will allow you to access all declared test dependencies and give you a natural place to write your test configuration.
1047
-
1048
-
To create a test launchable version of your application you should create an "`Application`" class in the `src/test` directory.
1049
-
For example, if your main application is in `src/main/java/com/example/MyApplication.java`, you should create `src/test/java/com/example/TestMyApplication.java`
1050
-
1051
-
The `TestMyApplication` class can use the `SpringApplication.from(...)` method to launch the real application:
1052
-
1053
-
include::code:launch/TestMyApplication[]
1054
-
1055
-
You'll also need to define the `Container` instances that you want to start along with your application.
1056
-
To do this, you need to make sure that the `spring-boot-testcontainers` module has been added as a `test` dependency.
1057
-
Once that has been done, you can create a `@TestConfiguration` class that declares `@Bean` methods for the containers you want to start.
1058
-
1059
-
You can also annotate your `@Bean` methods with `@ServiceConnection` in order to create `ConnectionDetails` beans.
1060
-
See <<features#features.testing.testcontainers.service-connections, the service connections>> section above for details of the supported technologies.
1061
-
1062
-
A typical Testcontainers configuration would look like this:
1063
-
1064
-
include::code:test/MyContainersConfiguration[]
1065
-
1066
-
NOTE: The lifecycle of `Container` beans is automatically managed by Spring Boot.
1067
-
Containers will be started and stopped automatically.
1068
-
1069
-
Once you have defined your test configuration, you can use the `with(...)` method to attach it to your test launcher:
1070
-
1071
-
include::code:test/TestMyApplication[]
1072
-
1073
-
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.
1074
-
1075
-
TIP: You can use the Maven goal `spring-boot:test-run` or the Gradle task `bootTestRun` to do this from the command line.
===== Contributing Dynamic Properties at Development Time
1081
-
If you want to contribute dynamic properties at development time from your `Container` `@Bean` methods, you can do so by injecting a `DynamicPropertyRegistry`.
1082
-
This works in a similar way to the <<features#features.testing.testcontainers.dynamic-properties,`@DynamicPropertySource` annotation>> that you can use in your tests.
1083
-
It allows you to add properties that will become available once your container has started.
1084
-
1085
-
A typical configuration would look like this:
1086
-
1087
-
include::code:MyContainersConfiguration[]
1088
-
1089
-
NOTE: Using a `@ServiceConnection` is recommended whenever possible, however, dynamic properties can be a useful fallback for technologies that don't yet have `@ServiceConnection` support.
A common pattern when using Testcontainers is to declare `Container` instances as static fields.
1096
-
Often these fields are defined directly on the test class.
1097
-
They can also be declared on a parent class or on an interface that the test implements.
1098
-
1099
-
For example, the following `MyContainers` interface declares `mongo` and `neo4j` containers:
1100
-
1101
-
include::code:MyContainers[]
1102
-
1103
-
If you already have containers defined in this way, or you just prefer this style, you can import these declaration classes rather than defining you containers as `@Bean` methods.
1104
-
To do so, add the `@ImportTestcontainers` annotation to your test configuration class:
1105
-
1106
-
include::code:MyContainersConfiguration[]
1107
-
1108
-
TIP: You can use the `@ServiceConnection` annotation on `Container` fields to establish service connections.
1109
-
You can also add <<features#features.testing.testcontainers.dynamic-properties,`@DynamicPropertySource` annotated methods>> to your declaration class.
===== Using DevTools with Testcontainers at Development Time
1115
-
When using devtools, you can annotate beans and bean methods with `@RestartScope`.
1116
-
Such beans won't be recreated when the devtools restart the application.
1117
-
This is especially useful for Testcontainer `Container` beans, as they keep their state despite the application restart.
1118
-
1119
-
include::code:MyContainersConfiguration[]
1120
-
1121
-
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`.
1122
-
With the default scope of `developmentOnly`, the `bootTestRun` task will not pick up changes in your code, as the devtools are not active.
1123
-
1124
-
1125
-
1126
1039
[[features.testing.utilities]]
1127
1040
=== Test Utilities
1128
1041
A few test utility classes that are generally useful when testing your application are packaged as part of `spring-boot`.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/dynamicproperties/MyContainersConfiguration.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/importingcontainerdeclarations/MyContainers.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/importingcontainerdeclarations/MyContainersConfiguration.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/launch/MyApplication.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/launch/TestMyApplication.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/test/MyApplication.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/test/MyContainersConfiguration.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/test/TestMyApplication.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/devtools/MyContainersConfiguration.kt
+1-1
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@
13
13
* See the License for the specific language governing permissions and
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/dynamicproperties/MyContainersConfiguration.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/importingcontainerdeclarations/MyContainersConfiguration.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/launch/MyApplication.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/launch/TestMyApplication.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/test/MyApplication.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/test/MyContainersConfiguration.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testcontainers/atdevelopmenttime/test/TestMyApplication.kt
0 commit comments