JUnit中有多个预期的异常 [英] Multiple expected exceptions in JUnit

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

问题描述

应用程序在thatMethod()中抛出2个不同的异常.我希望通过JUnit对其进行测试.

The application is throwing 2 different exceptions in some thatMethod(). I'm looking to test it by JUnit.

我可以使用 ExpectedException @Rule 来搁置应用程序可能引发的自然"异常. 您如何断言在JUnit 4测试中抛出某个异常?对此进行了解释.

I can use ExpectedException and @Rule to set aside a "natural" exception the application is expected to throw. How do you assert that a certain exception is thrown in JUnit 4 tests? is explaining this.

在我的情况下,我该如何做-2个或更多"自然"例外? ExpectedException 不包含多个预期的异常.

How can i do that in my case-- 2 or more "natural" exceptions? ExpectedException is not holding multiple expected exceptions.

还有其他方法可以执行此操作-如

There are other ways to do this-- as explained in How do you assert that a certain exception is thrown in JUnit 4 tests? again. i'm wondering whether there is a way to do this by using the built-in features of JUnit.

推荐答案

单个测试方法只能退出一次,因此只能抛出一个异常.如果您希望确认代码可以通过两种不同的方式失败,则可以进行两个单独的测试.将其分为两个测试方法,并在每个方法上声明特定的异常.

A single test method can only exit once, and so can only throw a single exception. If you wish to confirm that your code can fail in two different ways then you have two separate tests; split it into two test methods and declare the specific exception on each.

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

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