Jagadeesh is a DZone MVB and is not an employee of DZone and has posted 27 posts at DZone. You can read more from them at their website. View Full User Profile

TestNG @AfterClass Annotation Example

  • submit to reddit

TestNG methods that are annotated with @AfterClass annotation will be run after all the test methods in the current class have been run.

Here is a quick example


package com.skilledmonster.example;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
* Example to demonstrate use of @AfterClass annotation of TestNG framework
* @author Jagadeesh Motamarri
* @version 1.0
public class TestNGAnnotationAfterClassExample {
public void oneTimeTearDown() {
System.out.println("@AfterClass: The annotated method will be run after all the test methods in the current class have been run.");
public void validateSum() {
System.out.println("@Test : validateSum()");
int a = 5;
int b = 10;
Assert.assertEquals(a + b, 15);
public void validateDifference() {
System.out.println("@Test : validateDifference()");
int a = 5;
int b = 10;
Assert.assertEquals(b - a, 5);



As shown in the above console output, test method oneTimeTearDown() is executed after all the test methods in the class are executed.


 Download TestNG @AfterClass Example
Published at DZone with permission of Jagadeesh Motamarri, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Amir Pashazadeh replied on Sun, 2013/09/29 - 5:21pm

 You must be kidding us, by publishing this stuff!

Are these worth 4 distinct posts? What is it new in them that cannot be found in BASIC TestNG docs? What is it in them that annotation name does not say?

Jagadeesh Motamarri replied on Mon, 2013/10/07 - 6:53am in response to: Amir Pashazadeh

Amir, thanks for your feedback. I agree these posts on TestNG are pretty basics. But these have been automatically published by DZONE from my blog  from a tutorial list I was putting together on TestNG. Again, what you might think pretty basic might not be easy for others. These examples are targeted for newbies and I am pretty sure they are helpful to someone out there new to TestNG. Big or small, I like to put everything in action and do it myself. I will definitely try to bring you more interesting concepts and examples moving forward.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.