How to Test Java Application using TestNG? (original) (raw)

Last Updated : 8 Aug, 2025

To test a Java application using TestNG, you need to create test classes with test methods and define them in an XML configuration file. Here’s a simple Java application and a corresponding TestNG test suite.

**Step 1: Firstly, add dependencies in the Maven project pom.xml.

**pom.xml

XML `

<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 -->

`

**Step 2: 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.

**testng.xml

XML `

`

**Step 3: 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.

**CalculatorApplication.java

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;
}

}

`

**Step 4: 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");
}

}

`

**Step 5: 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 like as below:

Run the test file

**Step 6: Once the tests are run, in the console we will see the below output

Test 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

Test Success

Thus we can get the report in detail target\surefire-reports-Suite folder

Test Fail

Hence it is always better to do automated testing and via this, we can avoid many errors.