优雅地测试Exception:@Rule

使用@Rule测试Exception

  • 导入库
import org.junit.Rule;
import org.junit.rules.ExpectedException;
  • 声明错误期望对象
@Rule
public ExpectedException exception = ExpectedException.none();
  • 期望错误
  • 期望错误信息
exception.expect(Exception.class);
exception.expectMessage("......");
  • 最后执行程序

先期望错误,再执行程序

猜你喜欢

转载自blog.csdn.net/gzn00417/article/details/106129351