#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
TestNG & Maven

Adding Maven Surefire Plugin

What is the Surefire Plugin?

The Maven Surefire Plugin is responsible for actually running tests during the test phase of a Maven build and generating reports of the results.

Example Configuration

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.2.5</version>
  <configuration>
    <suiteXmlFiles>
      <suiteXmlFile>testng.xml</suiteXmlFile>
    </suiteXmlFiles>
  </configuration>
</plugin>

Why It's Needed

Without Surefire configured to point at testng.xml, running mvn test would not know to execute your TestNG suite.

How Surefire Decides What to Run

When you type mvn test, Maven walks through its lifecycle phases. It compiles code from src/main/java, compiles tests from src/test/java, and then hands control to Surefire in the test phase. Surefire then picks tests in one of two ways:

  • With a suite file: if suiteXmlFiles is configured, TestNG runs exactly the classes, groups and parameters listed in that XML.
  • Without a suite file: Surefire scans compiled test classes and, by default, includes names matching patterns such as Test*, *Test, *Tests and *TestCase. A class named LoginChecks would be silently skipped.

Surefire also detects the testing framework from the dependencies on the classpath, so having TestNG declared in pom.xml is what makes it use the TestNG provider.

Worked Example: Switching Suites From the Command Line

Teams often keep a quick smoke suite and a longer regression suite. Instead of editing the POM each time, store the suite name in a property:

<properties>
    <suiteXmlFile>testng.xml</suiteXmlFile>
</properties>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
        </suiteXmlFiles>
        <systemPropertyVariables>
            <browser>${browser}</browser>
        </systemPropertyVariables>
    </configuration>
</plugin>

Now the same project can run different suites and browsers:

mvn clean test                                    # default testng.xml
mvn clean test -DsuiteXmlFile=smoke.xml           # only the smoke suite
mvn clean test -DsuiteXmlFile=regression.xml -Dbrowser=firefox
mvn test -Dtest=LoginTest                          # one class, ignoring patterns
mvn test -Dtest=LoginTest#validLogin               # one method

Inside a test, System.getProperty("browser") reads the value passed through systemPropertyVariables. This is exactly how CI servers such as Jenkins choose a suite per job.

Reading the Results

After a run, Surefire writes reports to target/surefire-reports. The XML files there follow the JUnit report format, which CI tools read to draw pass and fail trends. TestNG adds its own HTML report and testng-results.xml in the same area. When a build fails, the console shows a summary of failed tests, and the matching .txt file holds the full stack trace.

Surefire Versus Failsafe, and Typical Mistakes

Surefire is meant for tests that should stop the build immediately when they fail. The Failsafe plugin runs in the integration-test and verify phases, so environments can be started before tests and cleaned up afterwards even when tests fail. Many teams keep fast unit checks in Surefire and slower end-to-end suites in Failsafe.

  • Tests placed in src/main/java: Surefire only looks at test sources, so nothing runs.
  • Suite file path wrong: the path is relative to the project root; a typo causes a build error or an empty run.
  • "Tests run: 0": usually a naming pattern mismatch, a missing TestNG dependency, or both JUnit and TestNG on the classpath confusing provider selection.
  • Hiding failures: setting testFailureIgnore to true keeps the build green even when tests fail; use it only when a later step reads the reports.
  • Very old plugin versions: they may not support current Java releases, so keep the version explicit and reasonably recent.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course