VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/testng-afterclass-annotations/

⇱ TestNG @AfterClass Annotations - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TestNG @AfterClass Annotations

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


Step 2: Create another TestNG Class After_Class2.Java.

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):

  • This method is annotated with @AfterClass, indicating that it should be executed after class execution.
  • It prints this " These are the type of frontend testing" statement.

Test Methods (@Test):

  • Each test method is annotated with @Test, indicating that it is a test case.
  • There are four test methods: fun1(), fun2(), fun3() and fun4().
  • Each test method prints their respective statement.

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.


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


Comment
Article Tags:

Explore