应用场景
验证一些异常场景时,比如传参传了非法的参数,预期结果接口应该抛错;实际执行结果抛错则认为是这条异常case执行成功。
比如如下代码
package com.course.testng;
import org.testng.annotations.Test;
public class exceptException {
@Test
public void testMethod() {
int a = 0;
int b = 10;
System.out.println(b / a);
}
}
执行后抛出错误java.lang.ArithmeticException: / by zero

我们的测试方法就是要验证被除数是0的时候,程序需要抛出ArithmeticException,如果程序正常抛出ArithmeticException,则测试用例执行通过
实现方式
用到注解@Test(expectedExceptions=异常类)
package com.course.testng;
import org.testng.annotations.Test;
public class exceptException {
@Test(expectedExceptions = ArithmeticException.class)
public void testMethod() {
int a = 0;
int b = 10;
System.out.println(b / a);
}
}
执行结果:

654

被折叠的 条评论
为什么被折叠?



