JUnit测试 - 分析预期的异常 [英] JUnit test — analysing expected Exceptions

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

问题描述



有没有办法分析这个异常?例如,我期待一个 CriticalServerException ,但我也想验证 getMessage 方法的内容。

解决方案

如果您有 JUnit 4.7 或以上,请尝试 ExpectedException



此问题,如下所示:

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

@Test
public void testRodneCisloRok(){
exception.expect(IllegalArgumentException.class);
exception.expectMessage(error1);
new RodneCislo(891415,dopocitej(891415));
}


In JUnit, I'm currently using annotation to expect an exception in my tests.

Is there a way to analyse this exception? For example, I expect a CriticalServerException, but I also want to verify the content of the getMessage method.

解决方案

If you have JUnit 4.7 or above try ExpectedException

There is an example in this question, which is copied below:

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

@Test
public void testRodneCisloRok(){
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("error1");
    new RodneCislo("891415",dopocitej("891415"));
}

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

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