我正在尝试测试除以零的功能。
如果我这样做:System.out.println(5/0),我会得到java.lang.ArithmeticException
我正在尝试测试是否引发了异常。
这是我的单元测试:
@Test(expected = ArithmeticException.class)
public void given_divideByZeroInput_when_divideByZero_then_verify_throwing_exception() {
MathClass sut = new MathClass();
sut.div(10,0);
}
我的div就是这么简单:
public float div(int x, int y) {
return (float) x/y;
}
但是,单元测试失败,指出:
java.lang.AssertionError: Expected exception: java.lang.ArithmeticException
我做错了什么?
我知道有很多方法可以测试抛出的异常,但我试图坚持使用简单的方法@Test(excepted = .. )
有只小跳蛙
相关分类