Open In App

TestNG @AfterMethod Annotations

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

In TestNG, there are 10 Annotations:

  1. @BeforeSuite
  2. @AfterSuite
  3. @BeforeTest
  4. @AfterTest
  5. @BeforeClass
  6. @AfterClass
  7. @BeforeMethod
  8. @AfterMethod
  9. @BeforeGroups
  10. @AfterGroups

In this article, we will learn about @AfterMethod.

What is @AfterMethod?

@AfterMethod is one of the TestNG Annotations. As the name defines, @AfterMethod is executed after each test method within a test class. Suppose there are n test methods within a test class, then n times @AfterMethod annotated method will be invoked. This annotation allows developers to specify various actions to be taken after test methods are run.

Example of @AfterMethod

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

Step 1: Open the Eclipse IDE.

Step 2: Create a Maven Project.

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

Screenshot-(306)

Package Explorer


Step 4. Create a TestNG Class that contain @AfterMethod.

package com.geeksforgeeks.test;

import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;

public class After_Method {

  @AfterMethod
  public void afterMethod() {
      System.out.println("This @AfterMethod will be executed after execution of each test method");
  }
  @Test
  public void firstMethod() {
      System.out.println("This is firstMethod");
  }
  @Test
  public void secondMethod(){
      System.out.println("This is secondMethod");
  }
  @Test
  public void thirdMethod() {
      System.out.println("This is thirdMethod");
  }
  @Test
  public void fourthMethod() {
      System.out.println("This is fourthMethod");
  }
  @Test
  public void fifthMethod() {
      System.out.println("This is fifthMethod");
  }
}


Now, let's explain what this code does:

Step 5: Now, we create the AnnotationsTest.xml file to configure the After_Method class.

<?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_Method"/>
                   <methods>
                        <include name="firstMethod" />
                     <include name="secondMethod" />
                     <include name="third Method" />
                     <include name="fourthMethod" />
                     <include name="fifth Method" />
                 </methods>
            
        </classes>
    </test>
</suite>


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

Output

Output of AfterMethod Annotations

Output of AfterMethod Annotations

As we can observe in above code, the execution of different tests or methods is in proper order. First fifthMethod() Test is executed, then firsthMethod() test, after that secondMethod() and at the end thirdMethod() will be executed.

Article Tags :