【发布时间】:2023-03-15 16:10:02
【问题描述】:
我正在使用 JUNIT 4。我尝试使用以下代码断言异常,但没有成功。
@Rule
public ExpectedException thrown = ExpectedException.none();
thrown.expect(ApplicationException.class);
但是,当我使用以下注释时,它可以工作并且测试通过了。
@Test(expected=ApplicationException.class)
如果我遗漏了什么,请告诉我。
import org.junit.Rule;
import org.junit.rules.ExpectedException;
public class Test
{
@Rule
public ExpectedException exception = ExpectedException.none();
@org.junit.Test
public void throwsIllegalArgumentExceptionIfIconIsNull()
{
exception.expect(IllegalArgumentException.class);
toTest();
}
private void toTest()
{
throw new IllegalArgumentException();
}
}
【问题讨论】:
-
你能发一个minimal reproducible example吗?
-
对不起,我意识到完整的真实示例会更容易调试。