<分区>
我正在使用 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();
}
}