How to Test Java Application using TestNG?
Last Updated :
26 Apr, 2025
TestNG is an automation testing framework widely getting used across many projects. NG means “Next Generation” and it is influenced by JUnit and it follows the annotations (@). End-to-end testing is easily handled by TestNG. As a sample, let us see the testing as well as the necessity to do it via a maven project.
Sample Maven Project
Project Structure:
This is a maven project. Hence TestNG dependencies need to be mentioned in pom.xml
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.8</version>
</dependency>
Always necessary dependencies need to be available in pom.xml
pom.xml
XML
<?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="https://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testNGSampleProject</groupId>
<artifactId>testNGSampleProject</artifactId>
<version>1.0</version>
<properties>
<!-- https://maven.apache.org/general.html#encoding-warning -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--Testing-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.8</version>
</dependency>
</dependencies>
<!-- Configure maven surefire plugin for qtest testng-plugin-log-collector
to listen the tests-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<!-- Jar file entry point -->
<addClasspath>true</addClasspath>
<mainClass>com.sample.CalculatorApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Following plugin executes the testng tests -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<encoding>iso-8859-1</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- End configuration -->
</project>
The main important files that we need to see are as follows :
testng.xml
Here we can specify the parameter and values that the testing file can accept. And also we need to specify n number of test java class files inside this, since as a whole, as a suite, test cases are going to get executed.
XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<!-- Parameters and their values are specified here -->
<parameter name="welcome" value="Geeky people" />
<parameter name="thankyou"
value="Geeky people" />
<!-- Specify number of test files under this tag -->
<classes>
<class name="com.sample.SampleTestProgram" />
<class name="com.sample.AdditionalTestProgram" />
</classes>
<!-- Specify number of test files under this tag -->
</test> <!-- Test -->
</suite> <!-- Suite -->
Let us start with the main business logic file which also is a standalone java application. But as a maven practice, we need to run it as a test as we are doing automated testing.
CalculatorApplication.java
It is a standard calculator program that contains separate methods for basic calculation as well as it contains steps to execute the TestNG code as well.
Java
import java.util.List;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.collections.Lists;
public class CalculatorApplication {
public static void main(String[] args) {
System.out.println("Calculation test via TestNg");
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.addListener(tla);
List<String> suites = Lists.newArrayList();
// path to xml.. This will refer the internal
// folder that contains the filename
suites.add("testng.xml");
testng.setTestSuites(suites);
testng.run();
}
public static int addNumbers(int one, int two) {
return one + two;
}
public static int subtractNumbers(int one, int two) {
return one - two;
}
public static int multiplyNumbers(int one, int two) {
return one * two;
}
public static int getQuotientByDividingNumbers(int one, int two) {
return one / two;
}
public static int getReminderByDividingNumbers(int one, int two) {
return one % two;
}
}
Let us test the same by adding two separate files
SampleTestProgram.java and AdditionalTestProgram.java
SampleTestProgram.java
Java
import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class SampleTestProgram {
@Test
@Parameters({ "welcome", "thankyou" })
public void testEasySamples(String welcome,String thankyou) {
String title = "welcome";
Assert.assertTrue(welcome.contains("Geeky people"));
Assert.assertTrue(thankyou.contains("Geeky people"));
Assert.assertTrue(title.contains("welcome"));
Assert.assertTrue((1000 * 20) == 20000);
Assert.assertTrue((1000 * 20) >= 2000);
Assert.assertEquals(true, title.contains("welcome"));
Assert.assertEquals(true, welcome.contains("Geeky people"));
Assert.assertEquals(true, thankyou.contains("Geeky people"));
}
@Test
public void testAddNumbers() {
Assert.assertTrue(300 == CalculatorApplication.addNumbers(100,200));
Assert.assertTrue(0 == CalculatorApplication.addNumbers(-100,100));
Assert.assertEquals(true, (0 == CalculatorApplication.addNumbers(-100,100)));
}
@Test
public void testSubtractNumbers() {
Assert.assertTrue(300 == CalculatorApplication.subtractNumbers(500,200));
Assert.assertTrue(-200 == CalculatorApplication.addNumbers(-100,-100));
Assert.assertNotEquals(true, (200 == CalculatorApplication.addNumbers(-100,-100)));
Assert.assertFalse(3000 == CalculatorApplication.subtractNumbers(500,200), "Subtracted result is wrong");
}
}
AdditionalTestProgram.java
Java
import org.testng.Assert;
import org.testng.annotations.Test;
public class AdditionalTestProgram {
@Test
public void testMultiplyNumbers() {
Assert.assertTrue(20000 == CalculatorApplication.multiplyNumbers(100,200));
Assert.assertTrue(0 == CalculatorApplication.multiplyNumbers(1000000,0));
Assert.assertEquals(true, (0 == CalculatorApplication.multiplyNumbers(0,200120)));
}
@Test
public void testGetQuotientByDividingNumbers() {
Assert.assertTrue(2 == CalculatorApplication.getQuotientByDividingNumbers(500,200));
Assert.assertTrue(1 == CalculatorApplication.getQuotientByDividingNumbers(-100,-100));
Assert.assertNotEquals(false, (2 == CalculatorApplication.getQuotientByDividingNumbers(500,200)));
Assert.assertFalse(3 == CalculatorApplication.getQuotientByDividingNumbers(500,200), "Quotient calculated result is wrong");
}
@Test
public void testGetReminderByDividingNumbers() {
Assert.assertFalse(1 == CalculatorApplication.getReminderByDividingNumbers(500,200));
Assert.assertTrue(0 == CalculatorApplication.getReminderByDividingNumbers(-100,-100));
Assert.assertNotEquals(true, (2 == CalculatorApplication.getReminderByDividingNumbers(-100,-100)));
Assert.assertFalse(3 == CalculatorApplication.getReminderByDividingNumbers(500,200), "Reminder calculated result is wrong");
}
}
Like this, we can add multiple test files and all should be included under testng.xml. Via the command line in the project folder, we can test the files as
mvn test
Or via eclipse as
Once the tests are run, in the console we will see the below output
In case of any errors, let us see how we can able to get the details. Under the target folder, the surefire-reports folder is available. Under the target\surefire-reports\junitreports folder, we can see the reports
Thus we can get the report in detail target\surefire-reports-Suite folder
Hence it is always better to do automated testing and via this, we can avoid many errors.
Similar Reads
TestNG Tutorial
TestNGÂ is an automation testing framework widely used across many projects. NG means âNext Generationâ and it is influenced by JUnit and it follows the annotations (@).Table of Content What is TestNG?PrerequisiteAdvantages of TestNGTestNG Basic To AdvanceConclusionFAQs on TestNG TutorialWhat is Test
3 min read
How to Install TestNG on Eclispse IDE?
Because of its design and inspired functionality, TestNG is often seen as a more versatile and powerful testing framework than JUnit and NUnit. Enabling TPM (Trusted Platform Module) and Secure Boot in the BIOS is essential for ensuring that your Windows 11 installation meets the security requiremen
5 min read
Running test cases in TestNG without java compiler
How to Test Java Application using TestNG?
TestNG is an automation testing framework widely getting used across many projects. NG means âNext Generationâ and it is influenced by JUnit and it follows the annotations (@). Â End-to-end testing is easily handled by TestNG. As a sample, let us see the testing as well as the necessity to do it via
4 min read
How to Execute Failed Test Cases in TestNG
TestNG is a testing framework that simplifies many testing needs. It stands for Test Next Generation, an open-source test automation framework inspired by JUnit and NUnit. Think of TestNG as an updated version of the other two concepts. It provides additional features not previously available, such
7 min read
Unit Testing, Integration Testing, Priority Testing using TestNG in Java
TestNG is an automated testing framework. In this tutorial, let us explore more about how it can be used in a software lifecycle. Unit Testing Instead of testing the whole program, testing the code at the class level, method level, etc., is called Unit Testing The code has to be split into separate
6 min read
How to use Regex in TestNG?
In this article, we will see how to use Regex in TestNG. In TestNG, we can use Regular Expressions for two purposes: To Include Test CasesTo Exclude Test Cases To include test cases we use the include the keyword in the testng.xml configuration file.To Exclude test cases we use the exclude keyword i
5 min read
TestNG Annotations in Selenium Webdriver with Examples
TestNG is a testing framework widely used in Selenium WebDriver for automation testing. It provides a wide range of annotations that help in organizing and controlling the flow of test cases. TestNG learns from JUnit and NUnit, making itself better by adding new features that make testing easier and
6 min read
TestNG Annotations - @BeforeSuite
The Concept Annotations is introduced in Java 1.5. The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn th
2 min read
TestNG Annotations - @AfterSuite
The Concept Annotations is introduced in Java 1.5. The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn th
2 min read
TestNG Annotations - @BeforeTest
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGrou
2 min read
TestNG @AfterTest Annotation
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGrou
3 min read
TestNG @BeforeClass Annotations
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGrou
3 min read