Did you know that TestNG offers a great feature that allows us to define annotations at the class level instead of just the method level? In this post, we will discuss class-level annotations in detail.
Let’s say we have 100 methods in one class, and writing the @Test annotation on each of these methods could be quite cumbersome. Instead, we can apply the @Test annotation at the class level. This way, all methods within the class will be treated as test methods, even if they aren’t individually annotated.
Below is our CodekruTest class, where we will be using the @Test annotation at the class level
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public class CodekruTest {
public void gammaMethod() {
System.out.println("Executing gammaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void alphaMethod() {
System.out.println("Executing alphaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void betaMethod() {
System.out.println("Executing betaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void deltaMethod() {
System.out.println("Executing deltaMethod in CodekruTest class");
Assert.assertTrue(true);
}
}
And below is the XML file that would run our CodekruTest class
<suite name="codekru">
<test name="codekruTest">
<classes>
<class name="Test.CodekruTest">
</class>
</classes>
</test>
</suite>
Output after running the above XML file –
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Now, what if we used an attribute with the @Test annotation at the class level? Would that also propagate to all of the test methods inside the class?
Let’s try to do this while using the invocationCount attribute here with the @Test annotation.
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test(invocationCount = 2)
public class CodekruTest {
public void gammaMethod() {
System.out.println("Executing gammaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void alphaMethod() {
System.out.println("Executing alphaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void betaMethod() {
System.out.println("Executing betaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void deltaMethod() {
System.out.println("Executing deltaMethod in CodekruTest class");
Assert.assertTrue(true);
}
}
Again run the same XML file and below will be the output –
Executing alphaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Here, we can see that each test method was executed two times.
What if we put the @Test annotation at the class level and at the method level too?
Here, the test method will have both the properties of the annotation at the class level and that of the method level, as shown in the below example.
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public class CodekruTest {
@Test(invocationCount = 2)
public void gammaMethod() {
System.out.println("Executing gammaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void alphaMethod() {
System.out.println("Executing alphaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void betaMethod() {
System.out.println("Executing betaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void deltaMethod() {
System.out.println("Executing deltaMethod in CodekruTest class");
Assert.assertTrue(true);
}
}
After running the same XML file
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Here, we can see that only gammaMethod was executed twice.
But now, what if we had the same attribute at the class level and on the method level too? What will happen then?
Here the method level attribute value will override the class level attribute value as shown in the below example
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test(invocationCount = 2)
public class CodekruTest {
@Test(invocationCount = 1)
public void gammaMethod() {
System.out.println("Executing gammaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void alphaMethod() {
System.out.println("Executing alphaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void betaMethod() {
System.out.println("Executing betaMethod in CodekruTest class");
Assert.assertTrue(true);
}
public void deltaMethod() {
System.out.println("Executing deltaMethod in CodekruTest class");
Assert.assertTrue(true);
}
}
Output after running the same XML file –
Executing alphaMethod in CodekruTest class
Executing alphaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing betaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing deltaMethod in CodekruTest class
Executing gammaMethod in CodekruTest class
Here, we can see that all of the test methods were executed twice except gammaMethod, which was executed only once.
Video Tutorial
We hope you liked the article. If you have any doubts or concerns, please feel free to write to us in the comments or email us at admin@codekru.com.