测试期望异常,异常被抛出(它显示在输出中),但测试失败 [英] Test expected an Exception, Exception was thrown (it shows in the output) but test failed anyway

查看:205
本文介绍了测试期望异常,异常被抛出(它显示在输出中),但测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  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())。包含(Rijbewijs.B)||!Arrays.asList(bestuurder.getRijbewijs())。contains(Rijbewijs.BE)){
抛出新的MensException(Geen correct rijbewijs);
} else {
this.bestuurder = bestuurder;
Ingezetenen.add(bestuurder);
}
男士[] a = ingezetenen;
if(a.length> Zitplaatsen - 1){
throw new MensException(te veel ingezetenen);
} else {
for(int i = 0; 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 
预期的异常:be.vdab.util.mens.MensException
junit.framework.AssertionFailedError:预期的异常:be.vdab.util.mens.MensException

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



编辑:我修复它不包括一个try-catch块,但只是抛出异常导致不得不在每个测试中添加抛出MensException它正在创建一个对象的方法。我通过调整我的自定义MensException来修复这个,而不是扩展异常我已经扩展了RuntimeException,所以我不必在每个测试方法中添加'throws MensException'。

解决方案

在你的方法中,你正在捕获异常并记录消息(这是一个糟糕的做法,你应该记录stacktrace),而在你的测试中你说出执行测试必须抛出 be.vdab.util.mens.MensException 而不被捕获。



只是在被测试的方法/构造函数中抛出它或者根本就没有。



选项1:



$ pre> public Voertuig(/ * ...你的参数在这里... * /){
this.nummerplaat = div.getNummerplaat();
this.Zitplaatsen = Zitplaatsen;
尝试{
// ...
// try ...
// ...
} catch(MensException e){
//System.out.println(e.getMessage());
//使用记录器,而不是System.out
//如果您还想使用System.out
//那么至少使用下面显示的代码
// e.printStackTrace(System.out的);
//上面的行被评论了,因为没有必要记录
//并重新抛出异常
//异常将被最高级执行
//处理应该记录或使用另一个策略
throw e;
}
}

选项2:

  public Voertuig(/ * ...你的参数在这里... * /){
this.nummerplaat = div.getNummerplaat();
this.Zitplaatsen = Zitplaatsen;
//删除try
//尝试{
// ...
//尝试...中的代码
// ...
//删除catch
//} catch(MensException e){
// System.out.println(e.getMessage());
//}
}

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


Hi so there's a test for a constructor for a vehicle. The test initializes a vehicle with a driver without a driving license and it should throw an Exception. code constructor:

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

code test:

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

and when i run this focused test method this is the outcome.

------------- Standard Output ---------------

Geen correct 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

So according to the Output the Exception is caught and displayed but yet the test failed. Anybody knows why? Thanks in advance.

EDIT: I fixed it by not including a try-catch block but just throwing the exception resulting in having to add 'throws MensException' in every test-method where it was creating an object. I fixed this by adjusting my custom MensException, instead of extending Exception i had it extend RuntimeException so i didn't have to add 'throws MensException' in every test-method.

解决方案

In your method, you're capturing the exception and logging the message (which is a bad practice, you should log the stacktrace) and in your test you state that the execution of the test must throw a be.vdab.util.mens.MensException without being catched.

Just re throw it or don't catch it at all in the method/constructor being tested.

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

Option 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 I would use option 2 rather than option 1.

这篇关于测试期望异常,异常被抛出(它显示在输出中),但测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆