Open In App

TestNG @BeforeMethod 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 @BeforeMethod.

What is @BeforeMethod?

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

Example of @BeforeMethod

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


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

CalculatorTest.java (@BeforeMethod)

package com.geeksforgeeks.test;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class CalculatorTest {

    NewTest calculator;

    @BeforeMethod
    public void setUp() {
        calculator = new NewTest();
        System.out.println("Calculator setup complete.");
    }

    @Test
    public void testAddition() {
        System.out.println("Addition Test:");
        int result = calculator.add(5, 3);
        System.out.println("Result: " + result);
    }

    @Test
    public void testSubtraction() {
        System.out.println("Subtraction Test:");
        int result = calculator.subtract(10, 4);
        System.out.println("Result: " + result);
    }

    @Test
    public void testMultiplication() {
        System.out.println("Multiplication Test:");
        int result = calculator.multiply(6, 7);
        System.out.println("Result: " + result);
    }

    @Test
    public void testDivision() {
        System.out.println("Division Test:");
        double result = calculator.divide(20, 4);
        System.out.println("Result: " + result);
    }
    
    public class NewTest {

        public int add(int a, int b) {
            return a + b;
        }

        public int subtract(int a, int b) {
            return a - b;
        }

        public int multiply(int a, int b) {
            return a * b;
        }

        public double divide(int dividend, int divisor) {
            if (divisor == 0) {
                throw new IllegalArgumentException("Cannot divide by zero");
            }
            return (double) dividend / divisor;
        }
    }
}


Now, let's explain what this code does:


Step 5: Now, we create the testng.xml file to configure the CalculatorTest 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.CalculatorTest"/>
               <methods>
                     <include name="testAddition" />
                    <include name="testSubtraction" />
                    <include name="testMultiplication" />
                    <include name="testDivision" />
              </methods>
        </classes>
    </test>
</suite>


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

Output

Output of BeforeMethod Annotations

Output of BeforeMethod Annotations

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

Article Tags :