Open In App

TestNG @BeforeClass Annotations

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

In this article, we will learn about @BeforeClass.

What is @BeforeClass?

@BeforeClass is one of the TestNG Annotations. As the name defines, @BeforeClass is executed before all the methods of the current class start their execution. This annotation allows developers to specify various actions to be taken before all the methods of the current class start their execution.

Example of @BeforeClass

Let's understand the @BeforeClass 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 @BeforeMethod.

1. Before_Class1.Java

package com.geeksforgeeks.test;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

public class Before_Class1 {
     @BeforeClass
      public void brforeClass() {
          System.out.println("Below are types 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");
      }
}


2. Before_Class2.Java

package com.geeksforgeeks.test;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

public class Before_Class2 {
      @BeforeClass
      public void beforeClass() {
          System.out.println("Below are types 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:

Step 5: Now, we create the AnnotationsTest.xml file to configure the Before_Class1 class and Before_Class2 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.Before_Class1" />
               <class name="com.geeksforgeeks.test.Before_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

Output of BeforeClass Annotations output

Output of BeforeClass Annotations

Article Tags :