我是單元測試新手,我正在嘗試學習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構造函數中拋出的。發佈的代碼中沒有構造函數。向我們顯示您的**實際**代碼 –