java 在 JUnit 中断言异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41031572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asserting exception in JUnit
提问by IUnknown
I need to write a JUnit test case which would test a function passing different permutations,with corresponding results.
A successful used case returns nothing ,while a failed permutation throws exception(exception type wouldnt matter).
我需要编写一个 JUnit 测试用例,它会测试一个传递不同排列的函数,并具有相应的结果。
成功的用例不返回任何内容,而失败的排列会引发异常(异常类型无关紧要)。
eg. testAppleisSweetAndRed(fruit,colour,taste)
The test would invoke the following -
例如。testAppleisSweetAndRed(fruit,colour,taste)
测试将调用以下内容 -
testAppleisSweetAndRed(orange,red,sweet)//throws exception
testAppleisSweetAndRed(apple,green,sweet)//throws exception
testAppleisSweetAndRed(apple,red,sour)//throws exception
testAppleisSweetAndRed(apple,red,sweet)//OK
If the invocations behave as expected,the test succeeds.
How would an assert trap the first 3 invocations to ensure they do raise expected exception?
如果调用的行为符合预期,则测试成功。
断言如何捕获前 3 次调用以确保它们确实引发预期异常?
回答by JoB?N
If you are using JUnit 4 or later you can do it as follows. you can use the
如果您使用的是 JUnit 4 或更高版本,您可以按如下方式进行。你可以使用
@Rule
public ExpectedException exceptions = ExpectedException.none();
this provides a lot of features which can be used to improve our JUnit tests.
If you see the below example I am testing 3 things on the exception.
这提供了许多可用于改进我们的 JUnit 测试的功能。
如果你看到下面的例子,我正在测试异常的 3 件事。
- The Type of exception thrown
- The exception Message
- The cause of the exception
- 抛出的异常类型
- 异常消息
- 异常的原因
public class MyTest {
@Rule
public ExpectedException exceptions = ExpectedException.none();
ClassUnderTest testClass;
@Before
public void setUp() throws Exception {
testClass = new ClassUnderTest();
}
@Test
public void testAppleisSweetAndRed() throws Exception {
exceptions.expect(Exception.class);
exceptions.expectMessage("this is the exception message");
exceptions.expectCause(Matchers.<Throwable>equalTo(exceptionCause));
testClass.appleisSweetAndRed(orange,red,sweet);
}
}
回答by Rahul Kumar
Tell your test method that what kind of exception your expecting form the test method. you just have to write like below syntax.
告诉您的测试方法,您期望从测试方法中产生什么样的异常。你只需要像下面的语法一样写。
@Test(expected = Exception.class)
It means i am expecting a Exception will be throw from the Test. you can use other exceptions as well like ArrayOutOfBound,etc..
这意味着我期望从测试中抛出异常。您也可以使用其他异常,例如 ArrayOutOfBound 等。
回答by Ole V.V.
I think the standard way when expected
is not adequate or not enough is to have an auxiliary method in you test class like:
我认为expected
不充分或不充分时的标准方法是在您的测试类中使用辅助方法,例如:
private static void testAppleIsSweetAndRedThrowsException(Fruit fruit, Colour colour, Taste taste) {
try {
testAppleisSweetAndRed(fruit, colour, taste);
fail("Exception expected from " + fruit + ", " + colour + ", " + taste);
} catch (Exception e) {
// fine, as expected
}
}
Now in your test you can write:
现在在你的测试中你可以写:
testAppleIsSweetAndRedThrowsException(orange, red, sweet);
testAppleIsSweetAndRedThrowsException(apple, green, sweet);
testAppleIsSweetAndRedThrowsException(apple, red, sour);