TestNG Hello World Example

In this post we will learn a very basic Hello World example using TestNG. We will learn how to setup environment to use testNG and how to actually write & execute unit test and verify results. We will be using maven based project.

Following environment being used:

  • TestNG 6.9.4
  • Maven 3
  • JDK 1.7
  • Eclipse JUNO Service Release 2

Project directory structure:

Following will be the maven based project directory structure for this example:

Let’s now add the content mentioned in above structure explaining each in detail.

Step 1: Add Dependency in pom.xml

Add TestNG dependency in 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">




Note the scope element set to test, as we are using testNG only for test purpose.

Step 2: Create Sample Java Class

Below is a sample Java class whose methods we will be testing in this post. Our class contains only one method which we will unit test.

package com.websystique.testng;

public class VatCalculator {
	 * Returns 21% VAT on given amount
	public double getVatOnAmount(double amount){
		return amount * 0.21;


Step 3: Create Test class

Below is a Test class used to unit-test the method getVatOnAmount from above Java class VatCalculator.

package com.websystique.testng;

import org.testng.annotations.Test;
import org.testng.Assert;

public class TestVatCalculator {

	public void testGetVatOnAmount(){
		VatCalculator calc = new VatCalculator();
		double expected = 21;
		Assert.assertEquals(calc.getVatOnAmount(100), expected);
		Assert.assertNotEquals(calc.getVatOnAmount(120), expected);

@Test annotation is what makes a method a test method. With TestNG, unit-testing a method involves calling that actual method in a test method annotated with @Test, asserting the output from actual method for success/failure. That’s what we did above. We called the getVatOnAmount on a real instance and then used TestNG Assert api to assert the return value from actual method. If any of the assertion fails, complete @Test will fail. Assert API provides dozens of method for comparing the expected result with the actual result.

Important points:
Source files are created under src/main/java and test files are created under src/test/java. It’s best practice to create the test files in a package created with same name as the package containing actual source files. Mimicing the same package name in test as in source keep things clean and understandable.

Name of test class must have ‘Test’ as prefix or suffix if you plan to execute the tests via maven (mvn test). This does not hold if you are running the test via eclipse plugin. Also, the Test class name should ideally reflect the source class name you are testing.

Name of test method can be anything. Ideally, name should reflect which method of source file you are testing. Including ‘test’ is not required in testcase name.

Step 4: Execute Tests

Tests can be executed in many different ways, some are…

  • Using TestNG Eclipse plugin
  • Using maven [ mvn test OR mvn install]

Using TestNG Eclipse plugin:
Install the plugin using Eclipse market place(search for testNG) or as mentioned on plugin page.
Once plugin installed successfully, right click on the test class and run as “TestNG Test”.

You will see following output:


Additionally, you will see that a folder named ‘test-output’ is now added to your project which contains different files related to results of unit test you preformed (specially testng-results.xml).

Using Maven :

Just browse through project using command line, perform mvn clean test. Maven uses maven-surefire-plugin to execute the tests.

E:\workspace7\TestNGHelloWorldExample>mvn clean test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building TestNGHelloWorldExample 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestNGHelloWorldExample ---
[INFO] Deleting E:\workspace7\TestNGHelloWorldExample\target
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNGHelloWorldExample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform d
[INFO] skip non existing resourceDirectory E:\workspace7\TestNGHelloWorldExample\src\main\resources
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ TestNGHelloWorldExample ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform depende
[INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\classes
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestNGHelloWorldExample --
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform d
[INFO] skip non existing resourceDirectory E:\workspace7\TestNGHelloWorldExample\src\test\resources
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ TestNGHelloWorldExample ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform depende
[INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\test-classes
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ TestNGHelloWorldExample ---
[INFO] Surefire report directory: E:\workspace7\TestNGHelloWorldExample\target\surefire-reports

 T E S T S
Running com.websystique.testng.TestVatCalculator
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@3a4346cd
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.976 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.150s
[INFO] Finished at: Fri Jul 03 20:26:37 CEST 2015
[INFO] Final Memory: 12M/226M
[INFO] ------------------------------------------------------------------------

You can see the report of executed tests above. Total test run were 1, out of which none has failed, no error, none skipped. Note that had tests been failed, the build would have failed. After the run, you will find a folder named ‘surefire-reports’ being added to your target folder, which contains unit-test result reports(specially testng-results.xml).

Note that maven test goal is anyway executed when you perform mvn clean install, so you may prefer to use mvn clean install instead of performing mvn clean test in order to execute unit test and install in one shot.

That’s it. As we mentioned before, it was a basic introduction to testNG setup. We will go through the testNG features in detail in subsequent posts.

Next post introduces you to commonly used TestNG annotations and how you can configure your test setup using them.


If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help me improve further our learning process.

If you appreciate the effort I have put in this learning site, help me improve the visibility of this site towards global audience by sharing and linking this site from within and beyond your network. You & your friends can always link my site from your site on www.websystique.com, and share the learning.

After all, we are here to learn together, aren’t we?