Home » Core Java » TestNG Configuration Annotations Example

About Ashraf Sarhan

Ashraf Sarhan
Ashraf Sarhan is a passionate software engineer, an open source enthusiast, has a Bsc. degree in Computer and Information Systems from Alexandria University. He is experienced in building large, scalable and distributed enterprise applications/service in multiple domains. He also has a keen interest in JavaEE, SOA, Agile and Big Data technologies.

TestNG Configuration Annotations Example

In this example we shall show you the TestNG Configuration Annotations. TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).

Annotations is one of the new functionalities that make it more powerful and easier. TestNG engine supports a series of annotations, with these annotations it even has stronger flexibility and extensibility, we will learn more about those annotations in our example.

 

1. TestNG Configuration Annotations:

  • Suite Annotations:
    • @BeforeSuite: The annotated method will be run before all tests in this suite have run.
    • @AfterSuite: The annotated method will be run after all tests in this suite have run.
  • Test Annotations:
    • @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
    • @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
  • Groups Annotations:
    • @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
    • @AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
  • Class Annotations:
    • @BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
    • @AfterClass: The annotated method will be run after all the test methods in the current class have been run.
  • Method Annotations:
    • @BeforeMethod: The annotated method will be run before each test method.
    • @AfterMethod: The annotated method will be run after each test method.

Tip

  • The order of execution of the above annotations in a typical TestNG case will be as the following:
    • @BeforeSuite->@BeforeGroups->@BeforeClass->@BeforeTest->@BeforeMethod
    • @AfterMethod->@AfterTest->@AfterClass->@AfterGroups->@AfterSuite
  • We can use those annotations to do some before/after configuration for your test class like setup or clean a database, preparing the dummy data, deploy or shut down the server and etc.
  • Suite test – Run multiple test classes together

2.TestNG Dependencies:

TestNG works out of the box without the need to download any additional plugins, you should add the following dependency in POM file of your Maven project.

    
<dependency>
     <groupId>org.testng</groupId>
     <artifactId>testng</artifactId>
     <version>6.1.1</version>
     <scope>test</scope>
</dependency>	

3. Example:

Take a look on the following examples to see the execution order of those methods which were annotated by the TestNG configuration annotations.

TestNGConfigurationAnnotationsTest.java:

package com.jcg;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNGConfigurationAnnotationsTest {
	
	@BeforeGroups("testGroup")
	public void beforeGroups() {
		System.out.println("@BeforeGroups");
	}
 
	@AfterGroups("testGroup")
	public void afterGroups() {
		System.out.println("@AfterGroups");
	}
 
	@BeforeClass
	public void beforeClass() {
		System.out.println("@BeforeClass");
	}
 
	@AfterClass
	public void afterClass() {
		System.out.println("@AfterClass");
	}
 
	@BeforeMethod
	public void beforeMethod() {
		System.out.println("@BeforeMethod");
	}
 
	@AfterMethod
	public void afterMethod() {
		System.out.println("@AfterMethod");
	}
 
	@Test(groups = "testGroup")
	public void runTest1() {
		System.out.println("@Test - runTest1");
	}
 
	@Test
	public void runTest2() {
		System.out.println("@Test - runTest2");
	}
	
}

Output:

Calling @BeforeClass
Calling @BeforeGroups
Calling @BeforeMethod
Calling @Test - runTest1
Calling @AfterMethod
Calling @AfterGroups
Calling @BeforeMethod
Calling @Test - runTest2
Calling @AfterMethod
Calling @AfterClass
PASSED: runTest1
PASSED: runTest2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

4. Download the Source Code of this example:

This was an example of TestNG Configuration Annotations.

Download
You can download the full source code of this example here: TestNGConfigAnnotationsExampleCode.zip
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of