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
If your compose file is not in the same directory as your application, or if it’s named differently, you can use configprop:spring.docker.compose.file[] in your `application.properties` or `application.yaml` to point to a different file.
173
182
Properties can be defined as an exact path or a path that’s relative to your application.
By default Spring Boot calls `docker compose up` when your application starts and `docker compose stop` when it's shut down.
232
241
If you prefer to have different lifecycle management you can use the configprop:spring.docker.compose.lifecycle-management[] property.
@@ -257,8 +266,8 @@ spring:
257
266
258
267
259
268
260
-
[[features.docker-compose.profiles]]
261
-
== Activating Docker Compose Profiles
269
+
[[features.dev-services.docker-compose.profiles]]
270
+
=== Activating Docker Compose Profiles
262
271
263
272
Docker Compose profiles are similar to Spring profiles in that they let you adjust your Docker Compose configuration for specific environments.
264
273
If you want to activate a specific Docker Compose profile you can use the configprop:spring.docker.compose.profiles.active[] property in your `application.properties` or `application.yaml` file:
@@ -271,3 +280,104 @@ spring:
271
280
profiles:
272
281
active: "myprofile"
273
282
----
283
+
284
+
285
+
286
+
[[features.dev-services.testcontainers]]
287
+
== Testcontainers Support
288
+
289
+
As well as xref:features/testing.adoc#features.testing.testcontainers[using Testcontainers for integration testing], it's also possible to use them at development time.
290
+
The next sections will provide more details about that.
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.
298
+
Using Testcontainers in this way provides functionality similar to Docker Compose, except that your container configuration is in Java rather than YAML.
299
+
300
+
To use Testcontainers at development time you need to launch your application using your "`test`" classpath rather than "`main`".
301
+
This will allow you to access all declared test dependencies and give you a natural place to write your test configuration.
302
+
303
+
To create a test launchable version of your application you should create an "`Application`" class in the `src/test` directory.
304
+
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`
305
+
306
+
The `TestMyApplication` class can use the `SpringApplication.from(...)` method to launch the real application:
307
+
308
+
include-code::launch/TestMyApplication[]
309
+
310
+
You'll also need to define the `Container` instances that you want to start along with your application.
311
+
To do this, you need to make sure that the `spring-boot-testcontainers` module has been added as a `test` dependency.
312
+
Once that has been done, you can create a `@TestConfiguration` class that declares `@Bean` methods for the containers you want to start.
313
+
314
+
You can also annotate your `@Bean` methods with `@ServiceConnection` in order to create `ConnectionDetails` beans.
315
+
See xref:features/testing.adoc#features.testing.testcontainers.service-connections[the service connections] section for details of the supported technologies.
316
+
317
+
A typical Testcontainers configuration would look like this:
318
+
319
+
include-code::test/MyContainersConfiguration[]
320
+
321
+
NOTE: The lifecycle of `Container` beans is automatically managed by Spring Boot.
322
+
Containers will be started and stopped automatically.
323
+
324
+
TIP: You can use the configprop:spring.testcontainers.beans.startup[] property to change how containers are started.
325
+
By default `sequential` startup is used, but you may also choose `parallel` if you wish to start multiple containers in parallel.
326
+
327
+
Once you have defined your test configuration, you can use the `with(...)` method to attach it to your test launcher:
328
+
329
+
include-code::test/TestMyApplication[]
330
+
331
+
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.
332
+
333
+
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
339
+
340
+
If you want to contribute dynamic properties at development time from your `Container` `@Bean` methods, you can do so by injecting a `DynamicPropertyRegistry`.
341
+
This works in a similar way to the xref:features/testing.adoc#features.testing.testcontainers.dynamic-properties[`@DynamicPropertySource` annotation] that you can use in your tests.
342
+
It allows you to add properties that will become available once your container has started.
343
+
344
+
A typical configuration would look like this:
345
+
346
+
include-code::MyContainersConfiguration[]
347
+
348
+
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.
356
+
Often these fields are defined directly on the test class.
357
+
They can also be declared on a parent class or on an interface that the test implements.
358
+
359
+
For example, the following `MyContainers` interface declares `mongo` and `neo4j` containers:
360
+
361
+
include-code::MyContainers[]
362
+
363
+
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.
364
+
To do so, add the `@ImportTestcontainers` annotation to your test configuration class:
365
+
366
+
include-code::MyContainersConfiguration[]
367
+
368
+
TIP: If you don't intend to use the xref:features/testing.adoc#features.testing.testcontainers.service-connections[service connections feature] but want to use xref:features/testing.adoc#features.testing.testcontainers.dynamic-properties[`@DynamicPropertySource`] instead, remove the `@ServiceConnection` annotation from the `Container` fields.
369
+
You can also add `@DynamicPropertySource` annotated methods to your declaration class.
==== Using DevTools with Testcontainers at Development Time
375
+
376
+
When using devtools, you can annotate beans and bean methods with `@RestartScope`.
377
+
Such beans won't be recreated when the devtools restart the application.
378
+
This is especially useful for Testcontainer `Container` beans, as they keep their state despite the application restart.
379
+
380
+
include-code::MyContainersConfiguration[]
381
+
382
+
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 `testAndDevelopmentOnly`.
383
+
With the default scope of `developmentOnly`, the `bootTestRun` task will not pick up changes in your code, as the devtools are not active.
0 commit comments