Reputation: 379
i have a class Calc which implements two methods add(int a, int b) and div(int a, int b) and a test class of this class:
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class CalcTest {
Calc c;
@BeforeClass
public void init() {
c = new Calc();
}
@Test(groups = "t1")
public void addTest() {
System.out.println("Testing add() method");
Assert.assertEquals(c.add(10, 5), 15);
}
@Test
public void divTest() {
System.out.println("Testing div() method");
Assert.assertEquals(c.div(10, 5), 2, 0);
}
@AfterClass
public void free() {
c = null;
}
}
and i have a testing.xml file to suite tests:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="first tests">
<test name="first test">
<groups>
<run>
<include name="t1" />
</run>
</groups>
<classes>
<class name="CalcTest" />
</classes>
</test>
</suite>
I just had a first look at the groups in testng so i would like to try it, butif i run testing.xml file i'm getting nullPointerException at line:
Assert.assertEquals(c.add(10, 5), 15);
-if i remove the "groups" annotation from the test method it works fine, thanks
Upvotes: 1
Views: 1450
Reputation: 91
pretty solution, as there might be more groups in the future, would be:
@BeforeClass(alwaysRun = true)
public void init() {
c = new Calc();
}
This causes your BeforeClass to run always, no matter what group you are running.
Upvotes: 2
Reputation: 8531
You need to keep your @BeforeClass annotation in the group. Add (groups = "t1") to your beforeclass annotation.
Upvotes: 2