我正在为构造函数编写 jUnit 测试,该构造函数解析字符串,然后检查许多内容。当数据错误时,对于每件事,都会抛出一些带有不同消息的 IllegalArgumentException 。
所以我想为它编写测试,但是我如何识别抛出了什么错误?
我该怎么做:
@Test(expected=IllegalArgumentException.class)
public void testRodneCisloRok(){
new RodneCislo("891415",dopocitej("891415"));
}
这就是我想要的,但我不知道是否可以以某种方式写它:
@Test(expected=IllegalArgumentException.class("error1"))
public void testRodneCisloRok(){
new RodneCislo("891415",dopocitej("891415"));
}
如果你有JUnit 4.7或以上您可以使用这种(优雅)方式:
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testRodneCisloRok(){
exception.expect(IllegalArgumentException.class);
exception.expectMessage("error1");
new RodneCislo("891415",dopocitej("891415"));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)