TestNG annotations – @BeforeMethod annotation

In this post, we will learn about the @BeforeMethod annotation in TestNG. @BeforeMethod annotated method will execute before every test case or method in the class but after the @BeforeClass annotated method. So, this means that @BeforeMethod annotated will run as many times as there are test cases in a particular class.

So, when does this @BeforeMethod annotated method will be executed?

Below XML will help you understand the @BeforeMethod annotated method’s execution point.

<suite name="codekru">
	<test name="codekru">
		<classes>
			<class name="Test.CodekruTestFirst">
				<methods>
					<!-- @BeforeMethod annotated method of CodekruTestFirst class will execute 
						here -->
					<include name="testMethod1" />
					<!-- @BeforeMethod annotated method of CodekruTestFirst class will again 
						execute here -->
					<include name="testMethod2" />
				</methods>
			</class>
			<class name="Test.CodekruTestSecond">
				<methods>
					<!-- @BeforeMethod annotated method of CodekruTestSecond class will 
						execute here -->
					<include name="testMethod1" />
					<!-- @BeforeMethod annotated method of CodekruTestSecond class will 
						again execute here -->
					<include name="testMethod2" />
				</methods>
			</class>
		</classes>
	</test>
</suite>

So, as shown in the above XML, the @BeforeMethod annotated method executes before every test case in the class ( or mentioned in the XML file). Let’s take an example to illustrate that point.

CodekruTestFirst.java

package Test;

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

public class CodekruTestFirst {

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("beforeMethod in CodekruTestFirst class");
	}

	@Test
	public void testMethod1() {
		System.out.println("Executing the test1 in CodekruTestFirst class");
		Assert.assertTrue(true);
	}

	@Test
	public void testMethod2() {
		System.out.println("Executing the test2 in CodekruTestFirst class");
		Assert.assertTrue(true);
	}

}

CodekruTestSecond.java

package Test;

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

public class CodekruTestSecond {

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("beforeMethod in CodekruTestSecond class");
	}

	@Test
	public void testMethod1() {
		System.out.println("Executing the test1 in CodekruTestSecond class");
		Assert.assertTrue(true);
	}

	@Test
	public void testMethod2() {
		System.out.println("Executing the test2 in CodekruTestSecond class");
		Assert.assertTrue(true);
	}

}

Now, we will run the XML mentioned above and see what happens.

The output of the XML file –

beforeMethod in CodekruTestFirst class
Executing the test1 in CodekruTestFirst class
beforeMethod in CodekruTestFirst class
Executing the test2 in CodekruTestFirst class
beforeMethod in CodekruTestSecond class
Executing the test1 in CodekruTestSecond class
beforeMethod in CodekruTestSecond class
Executing the test2 in CodekruTestSecond class

===============================================
codekru
Total tests run: 4, Failures: 0, Skips: 0
===============================================
So, what happened here?
  • As, By default, TestNG will run our tests in the order they are found in the XML file. So, it will pick CodekruTestFirst class and run its @BeforeMethod annotated method, its first test case, and then again, it will run the annotated method and then the second test case so on, until all of the cases are executed.
  • And then, it will come to the CodekruTestSecond class and execute it in the same manner.

The What If scenarios

What if we keep two @BeforeMethod annotated methods in a single class?

Let’s keep two @BeforeMethod annotated methods in CodekruTestFirst class and see what will happen when we run the class.

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

public class CodekruTestFirst {

	@BeforeMethod
	public void beforeMethod1() {
		System.out.println("beforeMethod1 in CodekruTestFirst class");
	}
	
	@BeforeMethod
	public void beforeMethod2() {
		System.out.println("beforeMethod2 in CodekruTestFirst class");
	}


	@Test
	public void testMethod1() {
		System.out.println("Executing the test1 in CodekruTestFirst class");
		Assert.assertTrue(true);
	}

	@Test
	public void testMethod2() {
		System.out.println("Executing the test2 in CodekruTestFirst class");
		Assert.assertTrue(true);
	}

}

Below is our XML file to run the CodekruTestFirst class

<suite name="codekru">
	<test name="codekruTest">
		<classes>
			<class name="Test.CodekruTestFirst" />
		</classes>
	</test>
</suite>

Output –

beforeMethod1 in CodekruTestFirst class
beforeMethod2 in CodekruTestFirst class
Executing the test1 in CodekruTestFirst class
beforeMethod1 in CodekruTestFirst class
beforeMethod2 in CodekruTestFirst class
Executing the test2 in CodekruTestFirst class

You can see that both @BeforeMethod annotated methods were executed before executing each test case in the class.

What if we place the @BeforeMethod annotated method on the superclass?

We will take two classes – CodekruTest(Base class) and CodekruTestSubclass(Derived class). CodekruTestSubclass extends the CodekruTest class, and CodekruTest class defines the @BeforeMethod annotated method.

package Test;

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

class CodekruTest {

	@BeforeMethod
	public void beforeMethod() {
		System.out.println("beforeMethod in CodekruTest class");
	}

	@Test
	public void test1() {
		System.out.println("Excecuting test in CodekruTest class");
		Assert.assertTrue(true);
	}

}

public class CodekruTestSubclass extends CodekruTest {

	@Test
	public void test2() {
		System.out.println("Executing test in CodekruTestSubclass");
		Assert.assertTrue(true);
	}

}

Below is our XML file used to run the CodekruTestSubclass.

<suite name="codekru">
	<test name="codekruTest">
		<classes>
			<class name="Test.CodekruTestSubclass" />
		</classes>
	</test>
</suite>

Output after running the XML file.

beforeMethod in CodekruTest class
Excecuting test in CodekruTest class
beforeMethod in CodekruTest class
Executing test in CodekruTestSubclass

Here, the methods were inherited by the CodekruTestSubclass, and thus we got the above output. If we make the methods of the superclass private, then they won’t be executed while running the CodekruTestSubclass.

Please visit this article to learn more about TestNG annotations.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Other TestNG annotations
Liked the article? Share this on

Leave a Comment

Your email address will not be published.