Open In App

TestNG @AfterMethod Annotations

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

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. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

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.

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

  • Package Declaration
    • The code is in the com.geeksforgeeks.test package.
  • Imports:
    • The code imports annotations and classes from the TestNG framework (org.testng.annotations.AfterMethod and org.testng.annotations.Test).
  • After_Method Class
    • This is the main test class.
    • It contains test methods and setup method.
  • afterMethod (@AfterMethod)
    • This method is annotated with @AfterMethod, indicating that it should be executed after each test method in the class.
    • It prints this ” @AfterMethod will be executed after execution of each test method” statement.
  • Test Methods (@Test):
    • Each test method is annotated with @Test, indicating that it is a test case.
    • There are five test methods: firstMethod(), secondMethod(), thirdMethod(), fourthMethod() and fifthMethod().
    • Each test method prints their respective statement.
  • After performing the operation, the result is printed to the console.

Step 5: Now, we create the AnnotationsTest.xml file to configure the After_Method 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_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

Screenshot30-(3)

Output


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads