Quick revision
The short answer
We import org.junit:junit-bom in dependencyManagement and add junit-jupiter with test scope, so every JUnit module gets the same version; then we pin a current Surefire 3.x for unit tests and add Failsafe with its integration-test and verify goals, which by default picks up classes named *IT. With a very old Surefire, before 2.22, the JUnit Platform is not supported, so the Jupiter tests are simply not found and the build reports zero tests and passes.
What to remember
In this answer, we set up JUnit 5 in Maven by importing junit-bom so that all JUnit modules share one version, adding junit-jupiter with test scope, pinning a current Surefire 3.x for *Test classes and adding Failsafe with integration-test and verify goals for *IT classes. Reports land in target/surefire-reports and target/failsafe-reports as XML that CI parses. A Surefire older than 2.22 does not know the JUnit Platform and silently runs zero tests, and with Spring Boot's parent the JUnit and plugin versions are already managed.
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 import org.junit:junit-bom in dependencyManagement and add junit-jupiter with test scope, so every JUnit module gets the same version; then we pin a current Surefire 3.x for unit tests and add Failsafe with its integration-test and verify goals, which by default picks up classes named *IT. With a very old Surefire, before 2.22, the JUnit Platform is not supported, so the Jupiter tests are simply not found and the build reports zero tests and passes.
Why this question is asked
Setting up JUnit 5 in Maven takes about thirty lines of XML, and almost every line has a reason. Getting it wrong rarely produces an error; more often it produces a green build that ran fewer tests than we think. Interviewers ask this because it checks practical build knowledge: version alignment through a BOM, the split between Surefire and Failsafe, and the classic trap of an old plugin version. In this answer, we will build the POM step by step, see what each plugin does in the lifecycle, look at where the reports go, explain what happens with an old Surefire, and finish with how Spring Boot's parent changes the picture.
1Aligning Versions With the JUnit BOM
JUnit 5 is several artifacts: junit-jupiter-api for writing tests, junit-jupiter-engine for running them, junit-jupiter-params for parameterized tests, and the junit-platform-* artifacts underneath. They must all have matching versions. A BOM, a bill of materials, is a POM that only lists versions. Importing it into dependencyManagement means we can declare JUnit dependencies without versions, and Maven fills in matching ones.
Notice that junit-jupiter is an aggregator: one dependency that brings the API, the parameterized tests module and the engine. Test scope keeps them off the production classpath. The BOM also protects us later: if some library pulls in an older junit-platform-engine transitively, the BOM's managed version wins, so the platform pieces stay consistent.
2Surefire for Unit Tests, Failsafe for Integration Tests
Surefire runs in the test phase and, by default, picks classes named Test*, *Test, *Tests or *TestCase. Failsafe runs in the integration-test phase and picks IT*, *IT and *ITCase. The split exists for a good reason: integration tests often need something started before them and stopped after them, such as a server or a container, and Failsafe's verify goal checks the results only after the post-integration-test phase has run, so a failing integration test does not skip the cleanup.
- compile and test-compile
- testSurefire runs *Test classes, fails fast
- packagethe jar is built
- pre-integration-teststart a server or container if needed
- integration-testFailsafe runs *IT classes, records results
- post-integration-teststop what was started
- verifyFailsafe fails the build if an IT failed
7 steps in order. Play them, or step through them one at a time.
Current Surefire and Failsafe versions detect the JUnit Platform on the test classpath and use it automatically; no provider configuration is needed. Pinning the version matters because Maven otherwise uses whatever default its own super POM names, and that default depends on the Maven version a developer happens to have.
3Where the Reports Go
Surefire writes its reports to target/surefire-reports and Failsafe to target/failsafe-reports. For every test class there is a TEST-<class>.xml file in the JUnit XML format and a short .txt summary. The XML files contain each test case with its time and any failure or skip, and they are what CI systems such as Jenkins, GitLab and GitHub Actions parse to show test results and trends. So the CI configuration should collect both directories; forgetting failsafe-reports is a common reason integration test failures do not show up in the CI test tab.
4What Happens With a Very Old Surefire
Surefire learned to run JUnit Platform tests natively in version 2.22.0. Older versions only know JUnit 3 and 4 and TestNG. If such a version runs a project whose tests use org.junit.jupiter.api.Test, it does not see them as tests at all. There is no error, because nothing is broken from its point of view.
This is the dangerous part: the build passes while running nothing. Old Maven 3 releases used a Surefire 2.12.4 default when the POM did not pin one, which is how many projects hit this after adding JUnit 5. The fix is to pin a current 3.x version, and the defence is to make zero tests a failure, for example with Surefire's failIfNoTests option in modules that are expected to have tests.
5Using Spring Boot's Parent
If the project uses spring-boot-starter-parent, or imports spring-boot-dependencies, Spring Boot already manages the JUnit version and the Surefire and Failsafe plugin versions, and spring-boot-starter-test brings junit-jupiter with it. In that case we should not import a different junit-bom as well, because two sources of versions are confusing. If we need a different JUnit version, Boot provides a property for it (junit-jupiter.version in recent generations; check your Boot version), and it is usually best to stay with the JUnit version that the Boot generation ships, since Spring's test support is tested against it.
6Follow-up questions
Why not put all tests in Surefire and skip Failsafe?
We can, and many projects do with tags instead. Failsafe adds two things: integration tests run after packaging, so they can test the built artifact, and the verify goal fails the build only after the post-integration-test cleanup has run. If tests manage their own containers with Testcontainers, the cleanup benefit matters less, and a tag-based split in Surefire is often simpler.
Do I still need junit-platform-launcher on the classpath?
Not for Maven: Surefire and Failsafe bring a launcher that matches the platform version they find. Gradle recommends adding junit-platform-launcher as a testRuntimeOnly dependency in recent versions, so that the launcher version matches the BOM.
7Conclusion
In this answer, we set up JUnit 5 in Maven by importing junit-bom so that all JUnit modules share one version, adding junit-jupiter with test scope, pinning a current Surefire 3.x for *Test classes and adding Failsafe with integration-test and verify goals for *IT classes. Reports land in target/surefire-reports and target/failsafe-reports as XML that CI parses. A Surefire older than 2.22 does not know the JUnit Platform and silently runs zero tests, and with Spring Boot's parent the JUnit and plugin versions are already managed.
Open with
I would import org.junit:junit-bom with import scope in dependencyManagement, and then add junit-jupiter with test scope and no version. The BOM keeps every JUnit artifact, the Jupiter API, params, engine and the platform modules, on the same version, which avoids mismatches when some library pulls in an older platform jar.
Then, if they want more
Then I pin a current Surefire 3.x, which detects the JUnit Platform automatically and runs classes ending in Test or Tests in the test phase. For integration tests I add the Failsafe plugin with the integration-test and verify goals.
It runs classes ending in IT in the integration-test phase, after packaging, and the verify goal fails the build only after post-integration-test, so any cleanup still runs. Reports go to target/surefire-reports and target/failsafe-reports as JUnit XML, and CI should collect both.
If someone uses a very old Surefire, before 2.22, it does not support the JUnit Platform at all, so it finds no Jupiter tests, prints Tests run: 0 and the build passes. That used to happen on old Maven versions whose default Surefire was 2.12.4, so I always pin the version and use failIfNoTests where a module must have tests.
With Spring Boot's parent, JUnit and the plugins are already managed, so I would not add a second BOM.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 5, JUnit 3, Java 17
- 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
- JUnit user guide
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 "Set up JUnit 5 in a Maven project with the BOM, Surefire for unit tests and Failsafe for...": https://scrivio-interview-library.netlify.app/java/junit/3f7d9a46a4/