How to execute JUnit and TestNG tests in same project using Maven-Surefire-Plugin?


Maven is a project management and comprehension tool that provides a complete build lifecycle framework. User can automate the project's build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle.

To summarize, Maven simplifies and standardizes the project build process. It handles compilation, distribution, documentation, team collaboration and other tasks seamlessly. Maven increases reusability and takes care of most of the build related tasks.

TestNG and Junit are testing framework and can use Maven as build tool. It helps to maintain dependencies and their version at one place in pom.xml

User can run Junit and TestNG test cases in maven using surefire plugin. However, it provides the flexibility to run it separately using same build.

In this tutorial, we will see how to run Junit and TestNG test cases in same project via surefire.

Approach/Algorithm to solve this problem

  • Step 1: Create a junit class − JunitTest

  • Step 2: Create a testng class − newTest

  • Step 3: Write @Test methods in both of the classes.

  • Step 4: Now add the testng and junit dependency in pom.xml

  • Step 5: Add 2 execution section in pom.xml. One for testng and other for junit as shown in program code section.

  • Step 6: Now, run the required execution to run either junit or testng testcases.

Syntax of command should be as :

mvn groupId:artifactId:versionId:goal@excution-id

Example

The following code to show how to run JUnit and TestNG test case in Maven:

src/ test/java/JunitTest.java

import org.junit.Test;

public class JunitTest {
    @Test
    public void testcase1() {
        System.out.println("in test case 1 of junit");
    }
} 

src/ test/java/newTest.java

import org.testng.annotations.Test;

public class newTest {

    @Test(groups = { "unit", "integration" })
    public void testCase1() {
        System.out.println("in test case 1 of newTest");
    }
    @Test(groups = { "integration" })
    public void testCase2() {
        System.out.println("in test case 2 of newTest");
    }
    @Test(groups = { "unit" })
    public void testCase3() {
        System.out.println("in test case 3 of newTest");
    }
}  

pom.xml

This is a maven configuration file that is used to organize dependencies, plugins and run the junit and testng test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>TestNGProjectct</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
<!-- configured to work with JUnit and TestNg in same project separatelly -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
<!-- skip the default execution, we have separate for TestNG and for JUnit -->
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>TestNGRun</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                            <includes>
                                <include>%regex[.*newTest.*]</include>
                            </includes>
                           <!-- used to skip JUnit provider -->
                            <junitArtifactName>null:null</junitArtifactName>
<!-- to continue on next execution in case of failures here -->
                            <testFailureIgnore>true</testFailureIgnore>
                        </configuration>
                    </execution>
                    <execution>
                        <id>JUnitRun</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>

                            <skip>false</skip>
                       <!-- used to skip TestNg provider -->
                            <testNGArtifactName>null:null</testNGArtifactName>
                            <excludes>
                                <exclude>%regex[.*newTest.*]</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
    </build>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>

    </properties>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

Command to Run TestNG testcases

mvn org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5::test@TestNGRun

Output

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------< com.sample:TestNGProjectct >-------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] ------------------------[ jar ]-------------------------
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (TestNGRun) @ TestNGProjectct ---
[INFO] 
[INFO] -----------------------------------------------------
[INFO]  T E S T S
[INFO] -----------------------------------------------------
[INFO] Running newTest
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
in test case 3 of NewTestngClass
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.547 s - in newTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] -------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------------
[INFO] Total time:  1.854 s
[INFO] Finished at: 2022-02-15T16:48:13+05:30
[INFO] --------------------------------------------------------

Process finished with exit code 0

Command to Run JUnit testcases

mvn org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5::test@JUnitRun

Output

[INFO] 
[INFO] ---------------------< com.sample:TestNGProjectct >---------------------
[INFO] Building TestNGProjectct 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (JUnitRun) @ TestNGProjectct ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running JunitTest
in test case 1 of junit
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 s - in JunitTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------
[INFO] Total time:  1.430 s
[INFO] Finished at: 2022-02-15T17:01:57+05:30
[INFO] --------------------------------------------------------

Process finished with exit code 0

Updated on: 16-Aug-2023

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements