测试预期异常,异常被抛出(它显示在输出中)但测试仍然失败

Test expected an Exception, Exception was thrown (it shows in the output) but test failed anyway

你好,这里有一个车辆构造器测试。该测试初始化​​一辆没有驾驶执照且 driver 的车辆,它应该抛出异常。 代码构造函数:

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);
}  

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

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

正确的 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

所以根据输出,异常被捕获并显示,但测试失败。有人知道为什么吗?提前致谢。

编辑:我通过不包含 try-catch 块但只是抛出异常导致必须在每个 test-method 中添加 'throws MensException' 来修复它,它正在创建一个 [=34] =].我通过调整我的自定义 MensException 来解决这个问题,而不是扩展 Exception 我让它扩展了 RuntimeException 所以我不必在每个 test-method.

中添加 'throws MensException'

这里:

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

您捕获了异常,因此它不会被抛出给测试 class。

更改为:

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

在你的方法中,你正在捕获异常并记录消息(这是一种不好的做法,你应该记录堆栈跟踪)并且在你的测试中你声明测试的执行 必须 抛出一个 be.vdab.util.mens.MensException 而没有被抓住。

在被测试的 method/constructor 中重新抛出或根本不抓住它。

选项 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 块中捕获异常。这就是为什么你的测试没有得到预期的异常而失败。