
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Test Case Using TestNG Annotations
Writing a test in TestNG basically involves the following steps ?
Write the business logic of the test and insert TestNG annotations in the code.
Add the information about the test (e.g. the class name, the groups you wish to run, etc.) in a testng.xml file or in build.xml.
Run TestNG.
In this article, we will see one complete example of TestNG testing using POJO class, Business logic class and a test xml, which will be run by TestNG.
Approach/Algorithm to solve this problem
-
Step 1: Create EmployeeDetails.java in src, which is a POJO class as shown in Program Code.
EmployeeDetails class is used to ?
get/set the value of employee's name.
get/set the value of employee's monthly salary.
get/set the value of employee's age.
-
Step 2: Create an EmpBusinessLogic.java in src, which contains business logic.
EmpBusinessLogic class is used for calculating ?
the yearly salary of employee.
the appraisal amount of employee.
-
Step 3: Create a TestNG class called TestEmployeeDetails.java in src. A TestNG class is a Java class that contains at least one TestNG annotation. This class contains test cases to be tested. A TestNG test can be configured by @BeforeXXX and @AfterXXX annotations as well.
TestEmployeeDetails class is used for testing the methods of EmpBusinessLogic class. It does the following ?
Tests the yearly salary of the employee.
Tests the appraisal amount of the employee.
-
Step 4: Now, create and run the testNG.xml or directly testNG class in IDE or compile and run it using command line.
In the output, the user can see expected and actual appraisal as well as salary.
Example
The following code for common TestNG class ? EmployeeDetails:
src/ EmployeeDetails.java
public class EmployeeDetails { private String name; private double monthlySalary; private int age; // @return the name public String getName() { return name; } // @param name the name to set public void setName(String name) { this.name = name; } // @return the monthlySalary public double getMonthlySalary() { return monthlySalary; } // @param monthlySalary the monthlySalary to set public void setMonthlySalary(double monthlySalary) { this.monthlySalary = monthlySalary; } // @return the age public int getAge() { return age; } // @param age the age to set public void setAge(int age) { this.age = age; } }
The following code for common TestNG class ? EmpBusinessLogic:
src/ EmpBusinessLogic.java
public class EmpBusinessLogic { // Calculate the yearly salary of employee public double calculateYearlySalary(EmployeeDetails employeeDetails) { double yearlySalary = 0; yearlySalary = employeeDetails.getMonthlySalary() * 12; return yearlySalary; } // Calculate the appraisal amount of employee public double calculateAppraisal(EmployeeDetails employeeDetails) { double appraisal = 0; if(employeeDetails.getMonthlySalary() < 10000) { appraisal = 500; } else { appraisal = 1000; } return appraisal; } }
The following code for common TestNG class ? TestEmployeeDetails:
src/ TestEmployeeDetails.java
import org.testng.Assert; import org.testng.annotations.Test; public class TestEmployeeDetails { EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic(); EmployeeDetails employee = new EmployeeDetails(); @Test public void testCalculateAppriasal() { employee.setName("Rajeev"); employee.setAge(25); employee.setMonthlySalary(8000); double appraisal = empBusinessLogic.calculateAppraisal(employee); Assert.assertEquals(500, appraisal, 0.0, "500"); System.out.println("Expected Appraisal:500;"+" Actual Appraisal:"+appraisal); } // Test to check yearly salary @Test public void testCalculateYearlySalary() { employee.setName("Rajeev"); employee.setAge(25); employee.setMonthlySalary(8000); double salary = empBusinessLogic.calculateYearlySalary(employee); Assert.assertEquals(96000, salary, 0.0, "8000"); System.out.println("Expected Salary:9600;"+" Actual Salary:"+salary); } }
testng.xml
This is a configuration file that is used to organize and run the TestNG test cases.
It is very handy when limited tests are needed to execute rather than full suite.
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1" > <test name = "test1"> <classes> <class name = "TestEmployeeDetails"/> </classes> </test> </suite>
Output
Expected Appraisal:500; Actual Appraisal:500.0 Expected Salary:9600; Actual Salary:96000.0 =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================