Friday, December 27, 2024
Google search engine
HomeLanguagesJavaHow to Test Java Application using TestNG?

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 a maven project.

Sample Maven Project

Project Structure:

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

Run the test file

 

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.

RELATED ARTICLES

Most Popular

Recent Comments