您可以将
@Rule注释与一起使用
ExpectedException,如下所示:
@Rulepublic ExpectedException expectedEx = ExpectedException.none();@Testpublic void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() throws Exception { expectedEx.expect(RuntimeException.class); expectedEx.expectMessage("Employee ID is null"); // do something that should throw the exception... System.out.println("=======Starting Exception process======="); throw new NullPointerException("Employee ID is null");}
请注意,
ExpectedException文档中的示例(当前)是错误的-
没有公共构造函数,因此您必须使用
ExpectedException.none()。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)