java - 使用 JUnit 理论时期望异常

标签 java testing exception-handling junit junit4

在 JUnit 4 中,您可以使用 @Test(expected = SomeException.class) 注释声明预期的异常。但是,当使用 Theories 进行测试时,@Theory 注释没有预期的属性。

测试理论时声明预期异常的最佳方式是什么?

最佳答案

我更喜欢使用 ExpectedException rule :

import org.junit.rules.ExpectedException;

<...>

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

@Theory
public void throwExceptionIfArgumentIsIllegal(Type type) throws Exception {
    assumeThat(type, equalTo(ILLEGAL));
    thrown.expect(IllegalArgumentException.class);
    //perform actions
}

关于java - 使用 JUnit 理论时期望异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13050412/

相关文章:

java - Google App Engine 上的 Spring Boot 应用程序 : unable to find main class

Python:Queue.Empty 异常处理

java - Spring MVC 中的嵌套异常处理

java - 我的 try 语句之后的所有内容都必须包含在该 try 语句中才能访问其中的变量吗?

java - 关闭 Spring @Service 的 Autowiring

Java EE i18n 和默认项目结构

java - 序列化 Integer 对象的大规模处理

node.js - Jest 提示错误……我告诉 Jest 要期待

ruby - 测试一个复杂的方法

javascript - 如何在 javascript 全局命名空间中 stub /模拟函数