TestNG @AfterClass Annotations (original) (raw)

Last Updated : 6 Aug, 2025

@AfterClass is one of the TestNG Annotations. As the name suggests, @AfterClass is executed after all the methods of the current class finish their execution. This annotation allows developers to specify various actions to be taken after all the methods of the current class finish their execution.

Let's understand the @AfterClass annotation through an example.

**Step 1: Create a Maven project and create a TestNG Class that contains @AfterMethod.

**After_Class1.Java

Java `

package com.geeksforgeeks.test;

import org.testng.annotations.Test; import org.testng.annotations.AfterClass;

public class After_Class1 {

@AfterClass public void afterClass() { System.out.println("These are type of frontend testing"); } @Test public void fun1() { System.out.println("Unit Testing"); } @Test public void fun2() { System.out.println("Integration Testing:"); } @Test public void fun3() { System.out.println("Regression Testing"); } public void fun4() { System.out.println("System Testing"); } }

`

**Step 2: Create another TestNG Class After_Class2.Java.

Java `

package com.geeksforgeeks.test;

import org.testng.annotations.Test; import org.testng.annotations.AfterClass;

public class After_Class2 { @AfterClass public void afterClass() { System.out.println("These are type of backend testing"); } @Test public void fun1() { System.out.println("Structural testing"); } @Test public void fun2() { System.out.println("Functional Testing"); } @Test public void fun3() { System.out.println("Non-Functional Testing"); } }

`

**Now, let's explain what this code does:

**Package Declaration: Both Class is on the com.geeksforgeeks.test package.

**Imports: Both Class imports annotations and classes from the TestNG framework (org.testng.annotations.AfterMethod and org.testng.annotations.Test).

**After_Class1 Class: This is the main test class. It contains test methods and after class method.

**afterClass (@AfterMethod):

**Test Methods (@Test):

After performing the operation, the result is printed to the console.

Similarly After_Class2. Java Class Execute

**Step 3: Now, we create the AnnotationsTest.xml file to configure the After_Class1 class and After_Class2 class.

XML `

`

**Step 4: Run the AnnotationsTest.xml. Right click on the AnnotationsTest.xml file, move the cursor down to Run As and then click on the TestNG Suite.

afterclass output testng

AfterClass Annotations output