gpt4 book ai didi

java - JUNIT - 断言异常

转载 作者:行者123 更新时间:2023-11-30 07:59:04 24 4
gpt4 key购买 nike

<分区>

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com