Open In App

TestNG @AfterClass Annotations

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG.

In TestNG, there are 10 Annotations

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test Case
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

In this article, we will learn about @AfterClass.

What is @AfterClass?

@AfterClass is one of the TestNG Annotations. As the name defines, @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.

Example of @AfterClass.

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

Step 1: Open the Eclipse IDE.

Step 2: Create a Maven Project.

Step 3. After Creating the Maven Project, the project exploration will look like the below image.


Screenshot-(306)

Package Explorer


Step 4. 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");
  }
}


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):
    • 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 5: Now, we create the AnnotationsTest.xml file to configure the After_Class1 class and After_Class2 class.

XML
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="suite">
    <test name="test1">
        <classes>
               <class name="com.geeksforgeeks.test.After_Class1"/>
               <class name="com.geeksforgeeks.test.After_Class2"/>
        </classes>
    </test>
</suite>


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

Output

outputa

Output




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads