【问题标题】:Test expected an Exception, Exception was thrown (it shows in the output) but test failed anyway测试预期异常,抛出异常(它显示在输出中)但测试仍然失败
【发布时间】:2015-01-23 20:28:03
【问题描述】:

您好,我们对车辆的构造函数进行了测试。该测试初始化​​了一辆没有驾驶执照的司机的车辆,它应该抛出一个异常。 代码构造函数:

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

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

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

【问题讨论】:

  • 你捕获了异常并处理了它。如果你期望一个异常,抛出它。还要确保注意该方法应该会抛出所述异常。

标签: java exception junit exception-handling


【解决方案1】:

在您的方法中,您正在捕获异常并记录消息(这是一种不好的做法,您应该记录堆栈跟踪)并且在您的测试中声明测试的执行必须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。

【讨论】:

  • 啊,谢谢,所以我实际上已经在构造函数中处理了异常,然后它就消失了。好的,谢谢大家提供的所有信息!
  • 记录或抛出,而不是两者。日志记录是“处理”异常。如果您登录并抛出它,它将被“处理”两次。
【解决方案2】:

您实际上是在 catch 块中捕获异常。这就是为什么您的测试没有得到预期的异常而失败的原因。

【讨论】:

    【解决方案3】:

    这里:

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

    你捕获了异常,所以它不会被抛出到测试类。

    改为:

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

    【讨论】:

    • 这是不好的做法。要么记录要么抛出,而不是两者兼而有之。日志记录是“处理”异常。如果您登录并抛出它,它将被“处理”两次。
    猜你喜欢
    • 2014-05-09
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 2014-05-10
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多