JUnit 测试预期异常的正确方法 [英] JUnit right way of test expected exceptions

查看:22
本文介绍了JUnit 测试预期异常的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想知道这种测试我的异常的方式是否可以,我有这个异常,我需要在第二个测试注释中抛出,结果我收到一个红色的邪恶条,以及成功和失败,就像你一样可以猜到失败是我的问题,我有一个失败();那里但原因是因为我读到那是测试异常的方法,现在我很困惑.

Hello guys I was wondering if this way of testing my exception is ok, i have this exception i need to throw in the second test annotation, im receiving as result a red evil bar, and a succeed and a failure, as you can guess the failure is my concern, i have a fail(); there but the reason is because i read thats the way to test the exception and now im confused.

另外我不得不说我不会得到绿色条,因为我期待异常,但我不知道失败是否是查看预期异常答案的正确方法.

Also i have to say im willin get the green bar because im expecting the exception, but i dont know if failure is the right way to see the answer of the expected exception.

如果您有任何建议,我将不胜感激

Also if you had any advice, I would appreciate it

@Before
    public void setUp() throws Exception {
        LogPack.logPacConfig(Constants.LOGGING_FILE);
        gtfri = "+RESP:GTFRI,380502,869606020101881,INCOFER-gv65,,10,1,1,0.0,0,888.1,-84.194560,9.955602,20170220074514,,,,,,0.0,,,,100,210100,,,,20170220074517,40A2$";
        weirdProtocol = "+RESP:GRI,380502,869606020101881,INCOFER-gv65,,10,1,1,0.0,0,888.1,-84.194560,9.955602,20170220074514,,,,,,0.0,,,,100,210100,,,,20170220074517,40A2$";
        factory = new LocomotiveFactory();
    }
    @Test
    public void GTFRICreationTester_shouldPass() throws TramaConProtolocoloDesconocido {
        assertTrue(factory.createLocomotive(gtfri, false, new Date()) instanceof LocomotiveGTFRI);
    }

    @Test(expected = TramaConProtolocoloDesconocido.class)
    public void GTFRICreationTester_shouldFail()  {
        try {
            factory.createLocomotive(weirdProtocol, false, new Date());
            fail("Expected an TramaConProtolocoloDesconocido");
        } catch (TramaConProtolocoloDesconocido e) {
            //assertSame("exception thrown as expected", "no se conoce el protocolo dado para la creacion de este factory", e.getMessage());;
        }
    }

推荐答案

测试预期异常的最常用方法有 3 种:

There is 3 most common ways to test expected exception:

第一种是最常见的方式,但您只能使用它测试预期异常的类型.如果 ExceptionType 不会被抛出,这个测试将会失败:

First one is the most common way, but you can test only the type of expected exception with it. This test will fail if ExceptionType won't be thrown:

@Test(expected = ExceptionType.class)
public void testSomething(){
    sut.doSomething();
}

您也无法使用此方法指定失败消息

Also you cannot specify the failure message using this approach

更好的选择是使用 ExpectedException JUnit @Rule.在这里你可以为预期的异常断言更多

The better option is to use ExpectedException JUnit @Rule. Here you can assert much more for expected exception

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testSomething(){
    thrown.expect(ExceptionType.class);
    thrown.expectMessage("Error message");
    thrown.expectCause(is(new CauseOfExeption()));
    thrown.reportMissingExceptionWithMessage("Exception expected"); 
    //any other expectations
    sut.doSomething();
}

第三个选项将允许您执行与使用 ExpectedException @Rule 相同的操作,但所有断言都应手动编写.然而,这种方法的优点是你可以使用任何你想要的自定义断言和任何断言库:

The third option will allow you to do the same as with using ExpectedException @Rule, but all the assertion should be written manually. However the advantage of this method is that you can use any custom assertion and any assertion library that you want:

@Test
public void testSomething(){
    try{
        sut.doSomething();
        fail("Expected exception");
    } catch(ExceptionType e) {
    //assert ExceptionType e
    } 
}

这篇关于JUnit 测试预期异常的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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