TestNG annotations – @BeforeClass annotation

In this post, we will discuss the @BeforeClass annotation in TestNG. @BeforeClass annotated method will run before the test cases of a particular class.

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

Below XML will help in understanding the execution point.

<suite name="codekru">
	<test name="codekru">  <!-- @BeforeTest will execute here -->
		<classes>
			<class name="Test.CodekruTestFirst" /> <!-- @BeforeClass for CodekruTestFirst will execute here -->
			<class name="Test.CodekruTestSecond" /> <!-- @BeforeClass for CodekruTestSecond will execute here -->
		</classes>
	</test>
</suite>

We can have multiple @BeforeClass annotated methods in a single test, just like we can have various classes in a single test as shown above in the XML file, and each class can have its own @BeforeClass annotated method.

Let’s look at the things with some action. I mean code 😛

We will take two classes, CodekruTestFirst and CodekruTestSecond, and each class will have its own @BeforeClass annotated method.

CodekruTestFirst.java

package Test;

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

public class CodekruTestFirst {

	@BeforeClass
	public void beforeClass() {
		System.out.println("beforeClass method in CodekruTestFirst class");
	}

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

}

CodekruTestSecond.java

package Test;

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

public class CodekruTestSecond {

	@BeforeClass
	public void beforeClass() {
		System.out.println("beforeClass method in CodekruTestSecond class");
	}

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

}

Now, we will run the below XML file and see what happens.

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="codekru">
	<test name="codekru">
		<classes>
			<class name="Test.CodekruTestFirst" /> <!-- @BeforeClass for CodekruTestFirst will execute here -->
			<class name="Test.CodekruTestSecond" /> <!-- @BeforeClass for CodekruTestSecond will execute here -->
		</classes>
	</test>
</suite>

Output –

beforeClass method in CodekruTestFirst class
Executing the test in CodekruTestFirst class
beforeClass method in CodekruTestSecond class
Executing the test in CodekruTestSecond class

===============================================
codekru
Total tests run: 2, Failures: 0, Skips: 0
===============================================

So what happened here? By default, TestNG will run our tests in the order found in the XML file. So, it got CodekruTestFirst class first and ran its @BeforeClass annotated method and then its test cases. And after that, it got CodekruTestSecond class, so TestNG first ran its @BeforeClass annotated method and then its test cases.

What will happen if we put more than one @BeforeClass annotated method in one class?

If we put more than one @BeforeClass annotated method in one class, all of the annotated methods will be executed. You can try this by putting one more annotated method in CodekruTestFirst class and rerunning the XML.

We have also written about the AfterClass annotation of TestNG, and if you want to learn more about TestNG annotations, please visit this article.

We hope that you have liked the article. If you have any doubts, 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.