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.
Step 4. Create a TestNG Class that contain @BeforeMethod.
CalculatorTest.java (@BeforeMethod)
package com.w3wiki.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:
- Package Declaration:
- The code is in the com.w3wiki.test package.
- Imports:
- The code imports annotations and classes from the TestNG framework (org.testng.annotations.BeforeMethod and org.testng.annotations.Test).
- CalculatorTest Class:
- This is the main test class.
- It contains test methods and setup method.
- NewTest Inner Class:
- This is a nested class inside CalculatorTest, which represents the calculator functionalities.
- It contains methods for basic arithmetic operations such as addition, subtraction, multiplication, and division. Below is the image of nested class.
- setUp Method (@BeforeMethod):
- This method is annotated with @BeforeMethod, indicating that it should be executed before each test method in the class.
- It creates an instance of the NewTest class, essentially initializing the calculator for each test method.
- The method prints “Calculator setup complete.” to the console.
- Test Methods (@Test):
- Each test method is annotated with @Test, indicating that it is a test case.
- There are four test methods: testAddition(), testSubtraction(), testMultiplication(), and testDivision().
- Each test method performs a specific arithmetic operation using the methods defined in the NewTest class.
- After performing the operation, the result is printed to the console.
- Output:
- When you run this test class, you’ll see output lines for each test method execution, along with the setup message from the setUp() method.
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.w3wiki.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
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.
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:
In this article, we will learn about @BeforeMethod.