Home » Enterprise Java » TestNG » TestNG Maven Project Example

About Ram Mokkapaty

Ram Mokkapaty
Ram holds a master's degree in Machine Design from IT B.H.U. His expertise lies in test driven development and re-factoring. He is passionate about open source technologies and actively blogs on various java and open-source technologies like spring. He works as a principal Engineer in the logistics domain.

TestNG Maven Project Example

In this article, I am going to show how to setup a Maven based project and run the TestNG tests.

Let’s first start with the setup.

  • Since this is about running TestNG tests using Maven as the build tool, it is obvious you need to have it.
  • I am using Eclipse as the IDE, version Luna 4.4.1.
  • Also install Maven-Eclipse plugin

We will go through each step and in the end we will run our TestNG tests using Maven.

1. About Maven and Surefire plugin

The core of Maven is very light. To execute its job, it depends on plugins and dependencies. When we run mvn install, it parses the build configuration, retrieves both the dependencies and plugins from the report repository. Maven Surefire plugin is responsible for running unit tests. If you want to write and execute unit tests, you need to first place them in ${basedir}/src/test/java. We also need to add a test-scoped dependency on TestNG and run mvn test

2. Download Maven and Install Maven-Eclipse Plugin

    1. Download Maven.
    2. Next is to create a Maven Project. If you haven’t used Maven before in Eclipse then you may also need to install the Maven-Eclipse plugin.
    3. You need to click on the Help->Install New Software. Enter http://download.eclipse.org/technology/m2e/releases in ‘Work with’ an click on Add.
    4. Select Maven plugin. Click on Next and then Finish.
Install Maven Eclipse Plugin

Install Maven Eclipse Plugin

3. Create Maven Project

    1. Once Maven Plugin is installed. You need to create Maven Project. If you are new to Maven, step by step details are here.
    2. In the Group Id, enter com.javacodegeeks.testng.maven. In artifact Id, enter the project name, for example, testNgMavenExample. Click on Finish to create the project.
Maven Project

Maven Project

4. Add TestNG Dependency to pom.xml

Now we will create a test class in src/test/java/com/javacodegeeks/testng/maven. Our test class contains a simple test method exampleOfTestNgMaven which prints a message.

TestNgMavenExampleTest:

package com.javacodegeeks.testng.maven;

import org.testng.annotations.Test;

public class TestNgMavenExampleTest {

	@Test
	public void exampleOfTestNgMaven() {
		System.out.println("This is TestNG-Maven Example");
	}
}

Since we haven’t downloaded the TestNG jar, you won’t be able to import any of the TestNG specific classes.
Now, you don’t have to specially download the jar. Remember you are using Maven, all you have to do is add TestNG dependency to your project model and maven will automatically add the jar to the project build path.

TestNG Jar Missing

TestNG Jar Missing

pom.xml:

<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.javacodegeeks.testng.maven</groupId>
  <artifactId>testngMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  	<dependencies>		
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.8.8</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

Once you add the dependency and save pom.xml, TestNG jar automatically gets downloaded. You can also verify it in Java Build Path properties.

TestNG jar in Java Build Path

TestNG jar in Java Build Path

5. Run test case using TestNG

    1. Run mvn test from Eclipse – right click on project, click on ‘Run As’ and then click on ‘Maven Test’
      Run Maven Test from  Eclipse

      Run Maven Test from Eclipse

    2. mvn test can also be run from the command line.

Output:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building testNgMavenExample 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testNgMavenExample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ testNgMavenExample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testNgMavenExample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ testNgMavenExample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testNgMavenExample ---
[INFO] Surefire report directory: C:\javacodegeeks_ws\testNgMavenExample\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.javacodegeeks.testng.maven.TestNgMavenExample
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@2b05039f
This is TestNG-Maven Example
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.269 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.902 s
[INFO] Finished at: 2015-02-27T18:28:14+05:30
[INFO] Final Memory: 8M/245M
[INFO] ------------------------------------------------------------------------

6. Run single test

Let’s introduce another test class TestNgMavenSecondClass.

TestNgMavenSecondClass:

package com.javacodegeeks.testng.maven;

import org.testng.annotations.Test;

public class TestNgMavenSecondClass {

	@Test
	public void oneMoreTest() {
		System.out.println("This is a TestNG-Maven based test");
	}
}

Now we have two test classes TestNgMavenExample and TestNgMavenSecondClass. Let’s first run both the tests from command line.

mvn test

Output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configur
ator@13f17c9e
This is TestNG-Maven Example
This is a TestNG-Maven based test
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.277 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.133 s
[INFO] Finished at: 2015-02-27T22:20:16+05:30
[INFO] Final Memory: 9M/307M
[INFO] ------------------------------------------------------------------------

If we want to run just the test class TestNgMavenSecondClass, we can do it using the following command:

mvn -Dtest=TestNgMavenSecondClass test

Output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.javacodegeeks.testng.maven.TestNgMavenSecondClass
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configur
ator@5f788551
This is a TestNG-Maven based test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.257 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.123 s
[INFO] Finished at: 2015-02-27T22:22:06+05:30
[INFO] Final Memory: 9M/307M
[INFO] ------------------------------------------------------------------------
C:\javacodegeeks_ws\testNgMavenExample>

7. Run the tests using testng.xml

Finally, you can also run the tests using testng.xml.

In the below testng.xml, I have combined both the test classes under one test.

In Eclipse, you need to right click on the Xml file and then click on ‘TestNG Suite’.

testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestNgMavenExampleSuite" parallel="false">
  <test name="TestNgMavenTest">
    <classes>
      <class name="com.javacodegeeks.testng.maven.TestNgMavenExample"/>
      <class name="com.javacodegeeks.testng.maven.TestNgMavenSecondClass"/>
    </classes>
  </test>
</suite>

Output:

[TestNG] Running:
  C:\javacodegeeks_ws\testNgMavenExample\src\test\resources\testng.xml

This is TestNG-Maven Example
This is a TestNG-Maven based test

===============================================
TestNgMavenExampleSuite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

8. Download the Eclipse Project

Download
You can download the full source code of this example here: testNgMavenExample.zip
(No Ratings Yet)
1 Comment Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Tani Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Tani
Guest
Tani

Very helpful tutorial. Cleanly explained. Thank you:)