Quick revision
The short answer
We define the containers once as @Bean methods annotated with @ServiceConnection in a @TestConfiguration, and add a small test main class that calls SpringApplication.from(Application::main).with(TestContainersConfig.class).run(args), which starts the real application wired to those containers. Spring Boot's Docker Compose support is an alternative for local runs. To keep CI fast, we reuse containers across test classes through static containers or a shared configuration, keep Testcontainers' reuse mode for local development, pre-pull images and keep them small.
What to remember
In this answer, we defined containers as @Bean methods with @ServiceConnection in a @TestConfiguration, started the real application with them through SpringApplication.from(Application::main).with(...) in a test main class, and compared that with Spring Boot's Docker Compose support. For CI we reused containers across test classes through shared configuration or static containers, kept Testcontainers' reuse mode for local development, and kept startup fast with pinned, pre-pulled and small images.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We define the containers once as @Bean methods annotated with @ServiceConnection in a @TestConfiguration, and add a small test main class that calls SpringApplication.from(Application::main).with(TestContainersConfig.class).run(args), which starts the real application wired to those containers. Spring Boot's Docker Compose support is an alternative for local runs. To keep CI fast, we reuse containers across test classes through static containers or a shared configuration, keep Testcontainers' reuse mode for local development, pre-pull images and keep them small.
Why this question is asked
Most services need a database, a broker or a cache to run, and new developers often lose their first day installing them. Meanwhile the test suite already knows how to start exactly the right versions in containers. Using the same definitions for local runs means one source of truth for infrastructure, and Spring Boot supports this directly. Interviewers ask this design question to see whether we know that support and can also keep the CI side efficient. In this answer, we will define the containers as beans, start the application with them, compare with Docker Compose, and then cover reuse and speed in CI.
1Define the Containers as Beans
Instead of declaring containers as static fields in a test class, we put them in a configuration class as @Bean methods. Spring then owns their lifecycle: a container is started when its bean is created and stopped when the context closes. @ServiceConnection on the bean method tells Spring Boot to create connection details from it, so the DataSource and Kafka clients connect without properties.
This class lives in src/test/java, so it never reaches the production artifact. Container class names and packages depend on the Testcontainers version, so check your version for the imports.
2Start the Application With Those Containers
Spring Boot 3.1 added SpringApplication.from(), which takes the application's real main method and lets us add extra configuration before running it. A test main class in src/test/java uses it to run the normal application plus our container configuration.
6 messages in order. Play them, or step through them one at a time.
Notice that the production code is untouched. A developer clones the repository, runs one command, and has the application running against the same database and broker versions the tests use. With Spring Boot DevTools, annotating a container bean with @RestartScope keeps it alive across automatic restarts, so data is not lost each time code changes.
3The Docker Compose Alternative
Spring Boot also offers Docker Compose support through the spring-boot-docker-compose module. When the application starts and finds a compose.yaml file, Boot runs docker compose up, detects known services such as PostgreSQL or Kafka, and creates connection details for them, much like @ServiceConnection. It is added as a development-only dependency.
| Aspect | Testcontainers beans | Docker Compose support |
|---|---|---|
| Definition lives in | Java test configuration | compose.yaml |
| Shared with tests | Yes, the same beans | No, tests define their own |
| Familiar to | Java developers | Anyone who knows Compose |
| Lifecycle | With the application context | docker compose up and stop |
| Good for | One source of truth with tests | Many services, non-Java tooling |
4Keeping CI Startup Fast: Reuse Across Test Classes
In CI the concern flips: we want containers to start as few times as possible. Declaring them as beans in a shared configuration that tests import means every test context with that configuration reuses the same containers. Static containers in a shared base class, started once per JVM, achieve the same thing. Either way, a suite of 200 test classes starts PostgreSQL once per JVM instead of 200 times.
Testcontainers also has a reuse mode, where a container marked withReuse(true) is left running after the JVM exits and found again next time, enabled per machine in the user's testcontainers properties file. It is mostly meant for local development, where it makes repeated test runs start instantly. On CI it is usually off, because runners are shared and short-lived and leftover containers would leak state between builds.
5Keeping CI Startup Fast: Images
Pulling a large image can take longer than everything else in a build. So we pin the versions in one shared configuration, pre-pull those images on CI runners or serve them from a registry mirror close to the runners, and prefer small variants, such as an alpine-based PostgreSQL image, where the behaviour is the same as production. Pinned versions also make the image cache effective: a floating tag like latest forces a fresh pull whenever it moves.
- Images pinned in shared configone place to upgrade
- Images pre-pulled on runnersor served by a mirror
- Containers start once per JVMshared beans or static fields
- Test contexts connect
@ServiceConnection, no properties - JVM endscontainers stopped, no reuse on CI
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Does the test main class affect production startup?
No. It lives under src/test/java and is only on the test classpath. The production main method runs exactly as before; the test main just adds configuration around it.
Can tests import the same TestContainersConfig?
Yes, and they should, with @Import(TestContainersConfig.class) on a shared base class. Then local runs and tests use identical definitions, and contexts that import it share the cached containers.
When would you prefer Docker Compose?
When the local setup includes things that are not Java tests' concern, such as a UI for the database, several services maintained by other teams, or developers who work in other languages. Compose is a common, tool-neutral format.
7Conclusion
In this answer, we defined containers as @Bean methods with @ServiceConnection in a @TestConfiguration, started the real application with them through SpringApplication.from(Application::main).with(...) in a test main class, and compared that with Spring Boot's Docker Compose support. For CI we reused containers across test classes through shared configuration or static containers, kept Testcontainers' reuse mode for local development, and kept startup fast with pinned, pre-pulled and small images.
Open with
Spring Boot supports this directly. I would put the containers in a @TestConfiguration as @Bean methods annotated with @ServiceConnection, for example PostgreSQL 17 and Kafka, so Spring owns their lifecycle and wires connection details without properties.
Then, if they want more
Then I add a test main class in src/test/java that calls SpringApplication.from with the real Application main method, adds the container configuration with with, and runs it. Developers start it with mvn spring-boot:test-run or gradle bootTestRun and get the application on its usual port, connected to the same database and broker versions the tests use, with no local installs.
With DevTools, @RestartScope keeps containers alive across restarts. The alternative is Spring Boot's Docker Compose support, which starts services from compose.yaml and creates connection details too; I would pick it if the team already maintains Compose files or works across languages.
For CI, the tests import the same configuration from a shared base class, so containers start once per JVM and are reused by every context instead of once per test class. Testcontainers' reuse mode I would keep for local development only, since CI runners are shared and short-lived.
And I would pin image versions in one place, pre-pull them on runners or use a mirror, and choose small images to keep startup fast.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.1
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference, Testing
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your developers want to run the application locally with the same containers used in...": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/faba4af6fd/