我是单元测试新手,我正在尝试学习JUnit。处理测试中的异常在我的项目中不起作用。这里是我的课:JUnit异常处理不起作用
public class Things {
private int amount;
private int[] weights;
private int[] values;
public Things(int amount, int[] weights, int[] values){
this.amount = amount;
if (amount!=weights.length || amount != values.length){
throw new IllegalArgumentException("Amount of things different than weights or values length");
}
this.weights=weights;
this.values=values;
}
}
和测试类:
public class ThingsTest {
@Test
public void testThingsConstructorIllegalArgumentsException(){
boolean exceptionThrown = false;
try{
Things thingsBadParams = new Things(5, new int[]{4,3,2}, new int[]{7,8});
}
catch (IllegalArgumentException e){
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
}
}
我知道,也许这不是处理异常的最好方式,但是这不是问题的关键。我可能已经想尽溶液(用@rule,@Test(预期= IllegalArgumentException.class),并且仍然在每次测试失败的时候,有一个红色的酒吧和说明它下面:
java.lang.IllegalArgumentException: Amount of things different than weights or values length
at pl.dsdev.Things.<init>(Things.java:14)
at ThingsTest.<init>(ThingsTest.java:11)
我。使用的IntelliJ IDEA 2017年,Maven和JUnit的4.12,我应该怎么做才能让测试成功
堆栈跟踪显示,你不执行你发布的代码。异常是从ThingsTest构造函数中抛出的。发布的代码中没有构造函数。向我们显示您的**实际**代码 –