VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/how-to-test-java-application-using-testng/

⇱ How to Test Java Application using TestNG? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Test Java Application using TestNG?

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

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

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

Step 4: Let us test the same by adding two separate files, SampleTestProgram.java and AdditionalTestProgram.java

SampleTestProgram.java

AdditionalTestProgram.java

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.

Comment
Article Tags:

Explore