Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A team's integration tests run with Surefire during 'mvn test' and leave a Docker database running whenever a test fails. How would you restructure the build so the environment is always torn down?

I would move the integration tests from Surefire to the Failsafe plugin, which binds to the integration-test and verify phases. The database is started in pre-integration-test and stopped in post-integration-test. Failsafe records failures during integration-test without failing the build, so post-integration-test still runs, and only the verify phase fails the build afterwards; that is why the team must run mvn verify, not mvn integration-test. Testcontainers, managed inside the tests, is an alternative that removes build-level setup altogether.

Why this question is asked

A test environment that is left running after a failure is more than an annoyance. On a developer laptop it blocks the port for the next run; on a shared CI agent it consumes memory, and the next build may connect to a database full of old data and pass or fail for the wrong reason. Interviewers ask this because the fix requires understanding exactly how Maven phases run and stop, and why Maven has a separate plugin for integration tests. In this answer, we will see why the current build leaks the container, restructure it with Failsafe, look at the configuration, explain which command to run, and compare the build-level approach with Testcontainers.

1Why the Database Is Left Running

Surefire is the plugin that runs unit tests in the test phase. When a test fails, Surefire fails the build at once, and Maven stops: no later phase runs. That behaviour is right for unit tests, because there is no point packaging code whose tests fail.

The team's build probably starts the Docker database in a phase before test, for example process-test-classes, and stops it in a phase after test, for example prepare-package. When every test passes, both run and all is well. When one test fails, the build stops inside the test phase, the stop goal in prepare-package never runs, and the container stays up. Nothing in the build is wrong in isolation; the stop step is simply in a place the build never reaches after a failure.

Step throughCurrent build: Surefire with a database around the test phase
  1. process-test-classes: start the database container
  2. test: Surefire runs the integration tests
  3. A test failed?
    Yesbuild fails immediately, later phases never runNocontinue
  4. prepare-package: stop the database containeronly reached when every test passed

4 steps in order. Play them, or step through them one at a time.

The teardown is skipped exactly when a test fails.

2The Failsafe Lifecycle Phases

The default lifecycle has four phases designed for this situation, all between package and install: pre-integration-test, integration-test, post-integration-test and verify. The Failsafe plugin is the integration-test counterpart of Surefire, and it splits its work across two goals. failsafe:integration-test, bound to the integration-test phase, runs the tests and writes the results to a file, but does not fail the build even when tests fail. failsafe:verify, bound to the verify phase, reads that file and fails the build if any test failed.

That split is the whole point. Because the integration-test goal never stops the build, Maven always continues to post-integration-test, where we stop the database. Only after teardown does verify report the failure.

Step throughAfter the fix: Failsafe with setup and teardown phases
  1. packagejar is built after unit tests pass
  2. pre-integration-test: start the database
  3. integration-test: Failsafe runs the *IT testsfailures are recorded, the build continues
  4. post-integration-test: stop the databasealways reached
  5. verify: Failsafe checks the resultsfails the build now if any IT failed

5 steps in order. Play them, or step through them one at a time.

3Restructuring the Build

First, the integration tests are renamed to match Failsafe's default patterns, such as OrderRepositoryIT, so that Surefire, whose defaults include *Test and *Tests, no longer picks them up. Then we configure Failsafe and move the start and stop goals of whatever manages the container into the new phases. Here the fabric8 docker-maven-plugin is used as an example; the image and plugin details should be checked against the current documentation.

XML
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal> <!-- runs *IT tests, records failures -->
<goal>verify</goal> <!-- fails the build afterwards -->
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<execution>
<id>start-db</id>
<phase>pre-integration-test</phase>
<goals><goal>start</goal></goals>
</execution>
<execution>
<id>stop-db</id>
<phase>post-integration-test</phase>
<goals><goal>stop</goal></goals>
</execution>
</executions>
</plugin>

Plugin versions belong in pluginManagement in the parent POM, which is why they are omitted here. A useful extra step is giving the container a random host port and passing it to the tests through a system property, so two builds on the same CI agent cannot collide.

4Run mvn verify, Not mvn integration-test

This detail is easy to miss. mvn integration-test runs phases only up to integration-test, so post-integration-test never runs and the database is left behind again, and verify never runs, so failed tests do not even fail the build. The command must reach at least verify: mvn verify, or mvn install, which includes verify.

The team should update its scripts and CI job definitions accordingly, and it is worth a comment in the POM, because someone will eventually try to save time with mvn integration-test.

Shell
$ mvn verify
[INFO] --- failsafe:integration-test (default) @ orders ---
[ERROR] Tests run: 12, Failures: 1
[INFO] --- docker:stop (stop-db) @ orders --- <- teardown still runs
[INFO] --- failsafe:verify (default) @ orders ---
[ERROR] BUILD FAILURE: There are test failures.

5The Alternative: Testcontainers Inside the Tests

Testcontainers is a Java library that starts Docker containers from test code. The tests themselves declare the database they need, and the library starts it before the tests and removes it afterwards. It also starts a small helper container, Ryuk, that removes the test containers if the JVM dies, so even a killed build does not leak them. Spring Boot can wire the container's connection details into the application with @ServiceConnection, so there is no configuration to copy.

With Testcontainers, the build needs no start and stop goals at all, and the same tests run identically from the IDE, where Maven phases are not involved. I would still run them with Failsafe as *IT classes, because keeping slow integration tests out of the test phase keeps the unit test loop fast. For a team starting fresh, this is usually the better design; the Failsafe phase restructuring is the right fix when the environment is genuinely build-level, such as several services started together.

Java
@SpringBootTest
@Testcontainers
class OrderRepositoryIT {
@Container
@ServiceConnection
static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:17");
@Autowired OrderRepository repo;
@Test void savesOrder() { /* ... */ }
}
// container started before the class, removed after it, even if a test fails

6Follow-up questions

Why not just add a stop step that runs on failure in the CI script?

It treats the symptom in one place only. Developers running the build locally would still leak containers, and the CI script becomes the only correct way to run the tests. Putting teardown in post-integration-test or in Testcontainers makes every run correct, wherever it happens.

Does Failsafe also run tests with the same JUnit setup as Surefire?

Yes. Both plugins share most of their code and configuration options and run JUnit 5 or 6 through the JUnit Platform. The differences are the default test name patterns, the phases they bind to and the split between running tests and checking results.

What if the start goal itself fails in pre-integration-test?

Then the build stops before any test runs, and post-integration-test does not run. Any partially started container may need cleanup, which is one more reason Testcontainers with its automatic cleanup is attractive.

7Conclusion

In this answer, we saw that the database leaks because Surefire fails the build inside the test phase, so the stop step in a later phase never runs. Moving the integration tests to Failsafe fixes this: we start resources in pre-integration-test and stop them in post-integration-test, Failsafe only records failures during integration-test, and its verify goal fails the build after teardown. The team must run mvn verify rather than mvn integration-test, and Testcontainers is a good alternative that manages the database from inside the tests.

What to say in the interviewAbout 80 seconds aloud

Open with

The problem is that Surefire runs in the test phase and fails the build immediately when a test fails, so any teardown bound to a later phase never runs and the container stays up.

Then, if they want more

I would move the integration tests to the Failsafe plugin, rename them to the IT suffix so Surefire ignores them, and use the phases Maven has for this: start the database in pre-integration-test and stop it in post-integration-test. Failsafe splits its work into two goals.

The integration-test goal runs the tests and records failures but does not fail the build, so Maven always continues to post-integration-test and tears the environment down. Then the verify goal reads the results and fails the build if anything failed.

That also means the team must run mvn verify, or install, and never mvn integration-test, because that stops before teardown and before the result check. As an alternative, I would seriously consider Testcontainers, where the tests themselves start the database, the library removes it afterwards, even if the JVM dies, and Spring Boot's ServiceConnection wires it up.

Then the build needs no start and stop goals, and the tests behave the same in the IDE. I would still run those tests through Failsafe to keep the unit test phase fast.

About this answer

Last updated
12 November 2026
Versions it names
JUnit 5
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
Apache Maven guides
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 "A team's integration tests run with Surefire during 'mvn test' and leave a Docker...": https://scrivio-interview-library.netlify.app/java/maven/cb8c09c8fb/