参数化测试类中的IllegalArgumentException

时间:2016-07-01 10:49:16

标签: rules illegalargumentexception parameterized-unit-test parameterized-tests

我有一个参数化测试类,我有一个测试方法,我希望它返回IllegalArgumentException。

@RunWith(value = Parameterized.class)
public class TriangleParametrizedTest {

    @Rule
    public ExpectedException exception = ExpectedException.none();

    enum Type {
        EQUILATERAL,
        NEGETIVE
    };

    private Type type;

    private int sideA;
    private int sideB;
    private int sideC;

    public TriangleParametrizedTest(Type type, int sideA, int sideB, int sideC) {
        this.type = type;
        this.sideA = sideA;
        this.sideB = sideB;
        this.sideC = sideC;
    }

    @Parameters()
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
            {Type.EQUILATERAL, 5, 5, 5},
            {Type.NEGETIVE, -5, 5, 5},});
    }

    @Test()
    public void negetiveSideTest() {
        Assume.assumeTrue(type == Type.NEGETIVE);
        exception.expect(IllegalArgumentException.class);
        Triangle trianle = new Triangle(sideA, sideB, sideC);

    }

}

但是我遇到了这个错误: 初始化错误:没有找到匹配方法negetiveSideTest ..

的测试

有任何解决方案吗?

1 个答案:

答案 0 :(得分:0)

使用@Test注释标记测试函数时,应该没有括号,如下所示:

@Test   <--- Remove the parenthesis here
public void negetiveSideTest() {
    Assume.assumeTrue(type == Type.NEGETIVE);
    exception.expect(IllegalArgumentException.class);
    Triangle trianle = new Triangle(sideA, sideB, sideC);
}

同样适用于@Parameters声明。