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

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

问题描述

在 JUnit 中,我目前使用注解来预期我的测试中出现异常.

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

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

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.

推荐答案

如果您有 JUnit 4.7 或更高版本,请尝试 ExpectedException

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天全站免登陆