如何在参数化测试中测试异常? [英] How do I test exceptions in a parameterized test?

查看:364
本文介绍了如何在参数化测试中测试异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JUnit4中,您可以通过在一个方法中提供参数集合来编写参数化单元测试,该方法将被传递给测试的构造函数并在另一个方法中进行测试。如果我有一个我希望抛出异常的参数,我该如何指定?

In JUnit4 you can write parameterized unit tests by providing parameters collection in one method, which will be passed to the constructor of the test and testing in another method. If I have a parameter for which I expect an exception to be thrown, how do I specify that?

推荐答案

if (parameter == EXCEPTION_EXPECTED) {
    try {
        method(parameter);
        fail("didn't throw an exception!");
    } catch (ExpectedException ee) {
        // Test succeded!
    }
}

这篇关于如何在参数化测试中测试异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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