Prev Next

DevOps / Maven Surefire Plugin Interview Questions

1. What is the Maven Surefire Plugin? 2. What is the difference between the Surefire and Failsafe plugins? 3. Which lifecycle phase does Surefire bind to by default? 4. How do you skip tests entirely when running a Maven build? 5. What's the difference between skipTests and maven.test.skip? 6. What does the testFailureIgnore configuration do? 7. How do you include or exclude specific test classes in Surefire? 8. What naming convention does Surefire use to auto-detect test classes? 9. How do you run a single test class from the command line? 10. Can wildcards be used with the -Dtest property? 11. How do you configure parallel test execution in Surefire? 12. What is forkCount and how does it differ from reuseForks? 13. What is the purpose of the argLine parameter? 14. Where does Surefire store its test result reports? 15. How do you generate an HTML report from Surefire's results? 16. Does the Surefire Plugin support TestNG out of the box? 17. How do you run JUnit 5 tests using Surefire? 18. How do you run only tests with a specific JUnit 5 tag or TestNG group? 19. How can you make Surefire automatically retry a failing test? 20. How do you set a timeout so a hung test doesn't block the build forever? 21. How does Surefire integrate with JaCoCo for code coverage? 22. How do you pass system properties into your tests through Surefire? 23. How do you set environment variables for the test JVM in Surefire? 24. How do you exclude tests by category or tag, not file name? 25. What happens if Surefire can't find any tests to run? 26. What does the useSystemClassLoader option control? 27. How do you control the order tests run in? 28. What does redirectTestOutputToFile do? 29. How do you keep integration tests separate from unit tests in a Surefire/Failsafe setup? 30. What is the testSourceDirectory element used for? 31. How do you debug tests that run through Surefire? 32. What does the printSummary configuration control? 33. What are the valid values for the reportFormat option? 34. How can you stop a multi-module build as soon as one module's tests fail? 35. How is Surefire typically configured across a multi-module Maven project? 36. What does the provider configuration control in Surefire? 37. Why might a ClassNotFoundException appear only when tests run via Surefire, not in the IDE? 38. What does a "Fork failed" or "Error occurred in starting fork" message usually mean? 39. How can you skip tests only within a specific Maven profile? 40. What's the difference between excludes and excludedGroups? 41. Can Surefire output test results directly as JSON for a custom dashboard? 42. Can Surefire run tests written in Kotlin or Groovy? 43. How does testFailureIgnore differ from wrapping test code in a try/catch? 44. How do you exclude certain tests only on a specific operating system? 45. How do you increase heap size for tests without touching the main build JVM? 46. How does Surefire distinguish a test failure from a test error? 47. Why might tests pass locally but fail in CI when using Surefire? 48. How should Surefire be integrated into a CI/CD pipeline effectively? 49. What notable improvements have appeared in recent Surefire Plugin versions?

1. What is the Maven Surefire Plugin?

The Surefire Plugin is Maven's standard plugin for executing unit tests during the build. It runs automatically in the test phase, invokes JUnit or TestNG tests found on the classpath, and writes results to target/surefire-reports in both plain-text and XML form. If any test fails, Surefire fails...

Read full answer

2. What is the difference between the Surefire and Failsafe plugins?

Surefire and Failsafe both run tests, but they target different stages of the build. Surefire Failsafe Runs unit tests in the test phase Runs integration tests in integration-test / verify A failing test fails the build immediately Failures are recorded but checked only in the verify goal, after ...

Read full answer

3. Which lifecycle phase does Surefire bind to by default?

The test goal binds to the test phase of Maven's default lifecycle. This places it after compile and test-compile , and before package , so compiled test classes are always available before Surefire runs, and a failure blocks packaging.

Read full answer

4. How do you skip tests entirely when running a Maven build?

Two command-line flags achieve this, but they behave differently. mvn install -DskipTests mvn install -Dmaven.test.skip=true -DskipTests still compiles the test sources, only the execution is skipped. -Dmaven.test.skip=true skips both compiling and running tests, which is faster but means broken ...

Read full answer

5. What's the difference between skipTests and maven.test.skip?

skipTests maven.test.skip Skips test execution only Skips compilation and execution Compile errors in tests still surface Compile errors in tests go unnoticed Most teams reach for skipTests when they still want confidence that test code compiles, reserving maven.test.skip for quick, throwaway loc...

Read full answer

6. What does the testFailureIgnore configuration do?

Setting true lets the Maven build continue past the test phase even when tests fail. The failures are still recorded in the surefire reports, so nothing is hidden — the build simply doesn't halt on them. This is common in CI pipelines that want a full ...

Read full answer

7. How do you include or exclude specific test classes in Surefire?

The and elements accept Ant-style path patterns: **/*ServiceTest.java **/*IntegrationTest.java Patterns are matched against the compiled test classes' rel...

Read full answer

8. What naming convention does Surefire use to auto-detect test classes?

By default Surefire looks for classes matching **/Test*.java , **/*Test.java , **/*Tests.java , and **/*TestCase.java . Anything outside this pattern is invisible to Surefire unless you explicitly add it via .

Read full answer

9. How do you run a single test class from the command line?

Use the -Dtest property with the class name: mvn test -Dtest=UserServiceTest You can target a single method the same way, using a # separator: mvn test -Dtest=UserServiceTest#shouldCreateUser .

Read full answer

10. Can wildcards be used with the -Dtest property?

Yes. -Dtest=*ServiceTest runs every class ending in ServiceTest , and -Dtest=com.example.service.* runs everything in that package. Multiple patterns can be comma-separated in a single invocation.

Read full answer

11. How do you configure parallel test execution in Surefire?

Use the element together with : methods 4 Valid values for parallel include methods , classes , both , and on the JUnit Platform, suites and all . Tests must be thread-safe for this to be...

Read full answer

12. What is forkCount and how does it differ from reuseForks?

forkCount controls how many separate JVM processes Surefire spawns to execute tests — for example 2 or 2C (twice the number of CPU cores). reuseForks is a boolean deciding whether one forked JVM is reused across multiple test classes or a fresh JVM is started for each class. Disabling reuse...

Read full answer

13. What is the purpose of the argLine parameter?

argLine passes JVM arguments directly to the forked process that runs the tests — heap size, system properties, or a Java agent: -Xmx1024m -javaagent:${jacoco.agent} It only affects the forked test JVM, not the JVM running the Maven buil...

Read full answer

14. Where does Surefire store its test result reports?

Under target/surefire-reports/ , with one .txt summary and one .xml file generated per test class. The XML format follows a schema widely understood by CI tools like Jenkins and GitLab CI for rendering pass/fail dashboards.

Read full answer

15. How do you generate an HTML report from Surefire's results?

Surefire itself only writes text and XML. To get an HTML report you add the separate maven-surefire-report-plugin and run mvn surefire-report:report , or bind it to the site phase. It reads the existing XML output and renders a browsable report.

Read full answer

16. Does the Surefire Plugin support TestNG out of the box?

Yes. Surefire auto-detects TestNG on the test classpath and switches its internal provider accordingly, no extra configuration required for basic execution. TestNG-specific options such as suiteXmlFiles , groups , and excludedGroups can be set directly in the plugin's .

Read full answer

17. How do you run JUnit 5 tests using Surefire?

You need Surefire 2.22 or later, which understands the JUnit Platform, plus junit-jupiter-engine on the test classpath. Once both are present, Surefire automatically selects the JUnit Platform provider — no explicit provider declaration is usually needed.

Read full answer

18. How do you run only tests with a specific JUnit 5 tag or TestNG group?

Use and : fast slow This maps to JUnit 5's @Tag annotation, or to TestNG's groups attribute when TestNG is the underlying framework.

Read full answer

19. How can you make Surefire automatically retry a failing test?

Set to a positive number: 2 Surefire will re-execute a failing test up to that many extra times before finally marking it failed. It's a pragmatic tool for known-flaky tests, but overusing...

Read full answer

20. How do you set a timeout so a hung test doesn't block the build forever?

Use . If the forked JVM running the tests exceeds this duration, Surefire kills it and fails the build, which is useful for catching infinite loops or deadlocks that would otherwise hang a CI agent indefinitely.

Read full answer

21. How does Surefire integrate with JaCoCo for code coverage?

JaCoCo works by injecting a Java agent into the JVM under test. Running jacoco:prepare-agent sets the argLine property with the agent's -javaagent flag, and Surefire automatically picks that property up when it forks the test JVM — no direct link between the two plugins beyond that shared p...

Read full answer

22. How do you pass system properties into your tests through Surefire?

staging /etc/app/config.yml Values set here appear as normal System.getProperty(...) calls inside the test JVM, letting you switch test behavior per environment without ...

Read full answer

23. How do you set environment variables for the test JVM in Surefire?

test Unlike systemPropertyVariables , these become actual OS-level environment variables visible via System.getenv() , which matters for code that specifically reads env vars rather than system pr...

Read full answer

24. How do you exclude tests by category or tag, not file name?

For JUnit 4, define a marker interface annotated with @Category , then reference it via . For JUnit 5, use the built-in @Tag annotation with directly — no marker interface needed. This differs from , which only matches class/file name patterns ra...

Read full answer

25. What happens if Surefire can't find any tests to run?

By default this fails the build with a "No tests were executed!" error. Setting false relaxes this, which is common in multi-module builds where some modules legitimately contain no tests and shouldn't break the reactor.

Read full answer

26. What does the useSystemClassLoader option control?

It decides whether the forked test JVM is launched using the system classloader directly, or via a manifest-only jar whose manifest lists the classpath. Some environments — notably Windows with very long classpaths — need the manifest-only jar approach to avoid command-line length lim...

Read full answer

27. How do you control the order tests run in?

The element accepts values like filesystem (default), alphabetical , reversealphabetical , random , hourly , and balanced , the last of which uses prior timing data to spread tests evenly across parallel forks.

Read full answer

28. What does redirectTestOutputToFile do?

Setting it to true redirects a test's System.out and System.err output into the corresponding report file instead of the build console, which keeps console output readable when a suite has hundreds of noisy tests.

Read full answer

29. How do you keep integration tests separate from unit tests in a Surefire/Failsafe setup?

Name integration test classes differently — typically ending in IT , such as *IT.java or IT*.java . Failsafe picks these up by default while Surefire's default patterns ignore them, giving a clean split without extra configuration on either plugin.

Read full answer

30. What is the testSourceDirectory element used for?

It tells Maven where test source files live if they're not in the conventional src/test/java location. It's rarely changed in standard projects, but useful for legacy or non-standard module layouts being migrated onto Maven.

Read full answer

31. How do you debug tests that run through Surefire?

mvn -Dmaven.surefire.debug test This pauses the forked test JVM and opens a debug listener, by default on port 5005, so you can attach a remote debugger from your IDE and step through test execution exactly as Surefire runs it.

Read full answer

32. What does the printSummary configuration control?

It toggles whether Surefire prints the familiar Tests run: X, Failures: Y, Errors: Z summary line to the console after execution. Disabling it is uncommon, but occasionally used to reduce console noise in highly automated pipelines that parse the XML reports instead.

Read full answer

33. What are the valid values for the reportFormat option?

brief (the default) gives a concise per-class summary, while plain gives a more detailed report including the full console output captured for each test method — useful when diagnosing a failure that needs more context than the brief summary provides.

Read full answer

34. How can you stop a multi-module build as soon as one module's tests fail?

Surefire has no dedicated fail-fast flag of its own, but combining Maven's own -ff (fail-fast) flag with a build where testFailureIgnore is not set will stop the reactor the moment a module's tests fail, rather than continuing to build unrelated modules.

Read full answer

35. How is Surefire typically configured across a multi-module Maven project?

Most teams declare Surefire once inside in the parent POM, so every child module inherits the same version, parallelism, and JVM settings automatically. Individual modules can still override specific values in their own section when they have unusual requirements.

Read full answer

36. What does the provider configuration control in Surefire?

The provider determines which test-framework integration Surefire uses internally — for example surefire-junit47 , surefire-testng , or surefire-junit-platform . Surefire normally auto-selects the correct provider from detected dependencies, but it can be forced explicitly when a project mi...

Read full answer

37. Why might a ClassNotFoundException appear only when tests run via Surefire, not in the IDE?

This usually points to a classpath mismatch between the forked test JVM and your IDE's own runtime — often caused by a dependency scoped as provided that tests actually need at runtime, shaded/relocated dependencies, or useSystemClassLoader settings that change how the classpath is assemble...

Read full answer

38. What does a "Fork failed" or "Error occurred in starting fork" message usually mean?

Common culprits are an invalid or missing JAVA_HOME , an argLine value that requests more memory than the machine has, security or antivirus software blocking new process creation, or a corrupted argLine expression left behind by an upstream plugin like JaCoCo.

Read full answer

39. How can you skip tests only within a specific Maven profile?

Bind a profile that sets the skipTests (or maven.test.skip ) property to true , then activate that profile explicitly with -P , or automatically via an block keyed on OS, a system property, or a file's presence.

Read full answer

40. What's the difference between excludes and excludedGroups?

excludes excludedGroups Matches file/class name patterns Matches tags, groups, or categories defined in the test code Example: **/*SlowTest.java Example: JUnit 5 @Tag("slow") , TestNG groups Use excludes when naming conventions already separate the tests you want to skip, and excludedGroups when ...

Read full answer

41. Can Surefire output test results directly as JSON for a custom dashboard?

Not natively — Surefire only produces plain-text and XML reports. Teams that need JSON typically either rely on CI-native XML parsers (the JUnit XML schema is widely supported already) or run a small post-build conversion step that transforms the XML into whatever format their dashboard exp...

Read full answer

42. Can Surefire run tests written in Kotlin or Groovy?

Yes. Surefire doesn't inspect source language at all — it only cares about compiled .class files that follow its discovery naming pattern and use a supported test framework. As long as Kotlin or Groovy test sources compile normally into the test output directory, Surefire runs them exactly ...

Read full answer

43. How does testFailureIgnore differ from wrapping test code in a try/catch?

testFailureIgnore operates at the build level: it lets the Maven build succeed despite failures without changing how the test itself behaves or reports. A try/catch inside a test method changes the test's own logic, and can silently swallow real failures — generally considered an anti-patte...

Read full answer

44. How do you exclude certain tests only on a specific operating system?

Combine Maven profiles with OS-based activation, for example windows , and apply a different list for Surefire inside that profile. This is common when certain tests only behave reliably on Linux CI agents.

Read full answer

45. How do you increase heap size for tests without touching the main build JVM?

Set -Xmx2g in Surefire's configuration. Because argLine only applies to the forked JVM that executes the tests, the memory settings of the JVM running Maven itself remain completely untouched.

Read full answer

46. How does Surefire distinguish a test failure from a test error?

A failure is a failed assertion — an AssertionError from JUnit or AssertJ, meaning the test ran but the expected condition wasn't met. An error is any other uncaught exception, such as a NullPointerException , meaning something broke unexpectedly before the assertion was even reached.

Read full answer

47. Why might tests pass locally but fail in CI when using Surefire?

The most frequent cause is hidden test-order dependency — tests that quietly rely on shared mutable state happening to run in a favorable sequence locally. A different runOrder , forkCount , or parallelism setting in CI can expose that coupling by running the same tests in a different order...

Read full answer

48. How should Surefire be integrated into a CI/CD pipeline effectively?

Keep the default fail-on-failure behavior rather than blanket-setting testFailureIgnore=true , publish the generated XML reports to the CI tool's native test-results viewer, and tune forkCount / parallel to balance speed against the runner's available CPU and memory. It also helps to fail fast on...

Read full answer

49. What notable improvements have appeared in recent Surefire Plugin versions?

Recent releases have strengthened native support for the JUnit Platform, reducing the need for the JUnit 4 vintage engine bridge in many setups, along with more stable parallel execution and improved fork/JVM lifecycle handling that plays better with modern JDK versions. Because default provider ...

Read full answer

«
»

Comments & Discussions