TestNG — Using Annotation

Emre Dogangun
3 min readMay 24, 2024

What is JUnit?

It is an open source framework for writing and running unit tests in Java.

For detailed information, you can go to this resource.

Annotations are like meta tags that you can add to your code and apply them to methods or class. These annotations in JUnit provide the following information about test methods -

  1. Information is given about which methods will work before and after the test methods.
  2. Information is given which methods run before and after all methods.
  3. Information is given which methods or classes to ignore during execution.

The following post provides a list of annotations and their meaning in JUnit.

Annotation

  1. @BeforeSuite: The method annotated with @BeforeSuite will execute prior to running all test methods within the suite.

2. @AfterSuite: The method annotated with @AfterSuite will execute after the completion of all test methods within the suite.

3. @BeforeTest: The method annotated with @BeforeTest will execute before running all test methods from the classes available in that directory.

4. @AfterTest: The method annotated with @AfterTest will execute after running all test methods from the classes available in that directory.

5. @BeforeClass: The method annotated with @BeforeClass will execute before the first method of the current class is called.

6. @AfterClass: The method annotated with @AfterClass will be invoked after executing all test methods of the current class.

7. @BeforeMethod: The method annotated with @BeforeMethod will execute before each test method runs.

8. @AfterMethod: The method annotated with @AfterMethod will execute after the execution of each test method.

9. @BeforeGroups: The method annotated with @BeforeGroups will run only once for a group before executing all test cases belonging to that group.

10. @AfterGroups: The method annotated with @AfterGroups will run only once for a group after executing all test cases belonging to that group.

To test the annotation, create a Java class file named TestNGAnnotation.java.

import org.testng.annotations.*;

public class TestNGAnnotation {

@BeforeSuite
public void before_suite() {
System.out.println("First method");
}

@AfterSuite
public void after_suite() {
System.out.println("Last method");

}

@BeforeTest
public void before_test() {
System.out.println("It will be executed first");
}

@AfterTest
public void after_test() {
System.out.println("After test execution..");
}

@BeforeClass
public void before_class() {
System.out.println("This method is executed before TestNGAnnotation");
}

@AfterClass
public void after_class() {
System.out.println("The above are the programming languages");
}

@BeforeMethod
public void before_method() {
System.out.println("This method will be invoked before every test method");
}

@AfterMethod
public void after_method() {
System.out.println("This method will be invoked after the execution of each test method");
}

@BeforeGroups("IT Department")
public void before_it() {
System.out.println("This method will be executed before the execution of IT Department group");
}

@Test
public void testcase1() {
System.out.println("HR");
}

@Test(groups = {"IT Department"})
public void testcase2() {
System.out.println("Software Developer");
}

@Test(groups = {"IT Department"})
public void testcase3() {
System.out.println("QA Analyst");
}

@AfterGroups("Testing tool")
public void after_group() {
System.out.println("The list which is shown above are the testing tools");
}

@Test(groups = {"Testing tool"})
public void testcase4() {
System.out.println("Appium");
}

@Test(groups = {"Testing tool"})
public void testcase5() {
System.out.println("JIRA");
}

@Test(groups = {"Testing tool"})
public void testcase6() {
System.out.println("RedMine");
}
}

Next, run the test cases.
Verify the output.

You can click on this link to access the project.

--

--