문제

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

도움이 되었습니까?

해결책

You need to keep your @BeforeClass annotation in the group. Add (groups = "t1") to your beforeclass annotation.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top