测试预期exception,exception被抛出(它显示在输出中)但是测试失败了

嗨所以有一个车辆构造函数的测试。 测试使用没有驾驶执照的驾驶员初始化车辆,并且应该抛出exception。 代码构造函数:

public Voertuig(String Merk, Datum datumEersteIngebruikname, int Aankoopprijs, int Zitplaatsen, Mens bestuurder, Mens ... ingezetenen) { this.nummerplaat = div.getNummerplaat(); this.Zitplaatsen = Zitplaatsen; try { this.Merk = Merk; this.datumEersteIngebruikname = datumEersteIngebruikname; this.Aankoopprijs = Aankoopprijs; if (!Arrays.asList(bestuurder.getRijbewijs()).contains(Rijbewijs.B) || !Arrays.asList(bestuurder.getRijbewijs()).contains(Rijbewijs.BE)) { throw new MensException("Geen correct rijbewijs"); } else { this.bestuurder = bestuurder; Ingezetenen.add(bestuurder); } Mens[] a = ingezetenen; if (a.length > Zitplaatsen - 1) { throw new MensException("te veel ingezetenen"); } else { for (int i = 0; i < a.length; i++) { ingezetenenExclBestuurder.add(a[i]); Ingezetenen.add(a[i]); } } } catch (MensException e) { System.out.println(e.getMessage()); } } 

代码测试:

  @Test(expected = be.vdab.util.mens.MensException.class) public void test_constructor_zonder_Rijbewijs() { //VOERTUIG B,BE//bestuurder:--- Voertuig voertuig = new TestVoertuig("auto", datum, 18300, AANTAL_INZITTENDEN, INGEZETENE_A); } 

当我运行这种集中测试方法时,这就是结果。

————-标准输出—————

Geen纠正了rijbewijs


 Testcase: Testcase: test_constructor_zonder_Rijbewijs(be.vdab.voertuigen.VoertuigTest): FAILED Expected exception: be.vdab.util.mens.MensException junit.framework.AssertionFailedError: Expected exception: be.vdab.util.mens.MensException 

因此,根据输出,捕获并显示exception,但测试失败。 谁知道为什么? 提前致谢。

编辑:我通过不包括try-catch块修复它,但只是抛出exception导致必须在创建对象的每个测试方法中添加’throws MensException’。 我通过调整我的自定义MensException来修复它,而不是扩展Exception我有它扩展RuntimeException所以我不必在每个测试方法中添加’throws MensException’。

在您的方法中,您捕获exception并记录消息(这是一种不好的做法,您应该记录be.vdab.util.mens.MensException )并在测试中声明测试的执行必须抛出be.vdab.util.mens.MensException没有被be.vdab.util.mens.MensException

只是在被测试的方法/构造函数中抛出它或者根本不捕获它。

选项1:

 public Voertuig(/* ...your arguments here... */) { this.nummerplaat = div.getNummerplaat(); this.Zitplaatsen = Zitplaatsen; try { //... //code in the try... //... } catch (MensException e) { //System.out.println(e.getMessage()); //use a logger, not System.out //in case you still want to use System.out //then at least use the code shown below //e.printStackTrace(System.out); //line above commented since there's no need to log //and rethrow the exception //the exception will be handled by the highest level execution //and there it should be logged or use another strategy throw e; } } 

选项2:

 public Voertuig(/* ...your arguments here... */) { this.nummerplaat = div.getNummerplaat(); this.Zitplaatsen = Zitplaatsen; //remove the try // try { //... //code in the try... //... //remove the catch // } catch (MensException e) { // System.out.println(e.getMessage()); // } } 

IMO我会使用选项2而不是选项1。

您实际上是在catch块中捕获exception。 这就是为什么你的测试未能得到预期的exception。

这里:

  } catch (MensException e) { System.out.println(e.getMessage()); } 

您捕获exception,因此不会抛出测试类。

更改为:

 } catch (MensException e) { System.out.println(e.getMessage()); throw e }