断言异常被抛出

huangapple go评论113阅读模式
英文:

Assert an Exception is Thrown in

问题

我在我的项目中有这个JUnit测试:

  1. public class CalculatorBookingTest {
  2. private CalculatorBooking calculatorBooking;
  3. @Rule
  4. public ExpectedException expectedException = ExpectedException.none();
  5. @Before
  6. public void setUp() {
  7. calculatorBooking = new CalculatorBooking();
  8. }
  9. @Test
  10. public void shouldThrowAnException_When_InputIsNull() {
  11. calculatorBooking.calculate(null, null, 0, null);
  12. expectedException.expect(CalculationEngineException.class);
  13. expectedException.expectMessage("Error");
  14. }
  15. }

但当我运行这个测试时,异常被抛出,但测试仍然失败。

英文:

I have this Junit test in my project

  1. public class CalculatorBookingTest {
  2. private CalculatorBooking calculatorBooking;
  3. @Rule
  4. public ExpectedException expectedException = ExpectedException.none();
  5. @Before
  6. public void setUp() {
  7. calculatorBooking = new CalculatorBooking();
  8. }
  9. @Test
  10. public void shouldThrowAnException_When_InputIsNull() {
  11. calculatorBooking.calculate(null, null, 0, null);
  12. expectedException.expect(CalculationEngineException.class);
  13. expectedException.expectMessage("Error");
  14. }
  15. }

but when I run the test, the Exception is Thrown but nevertheless the test fail

答案1

得分: 2

你可以这样做:

  1. @Test(expected = CalculationEngineException.class)
  2. public void shouldThrowAnException_When_InputIsNull() {
  3. calculatorBooking.calculate(null, null, 0, null);
  4. }

从 Junit 文档 中:

> Test 注解支持两个可选参数。第一个参数 expected 声明了一个测试方法应该抛出一个异常。如果它没有抛出异常,或者抛出了一个不同于声明的异常,测试将失败。

英文:

You could do something like:

  1. @Test(expected = CalculationEngineException.class)
  2. public void shouldThrowAnException_When_InputIsNull() {
  3. calculatorBooking.calculate(null, null, 0, null);
  4. }

From Junit doc :

> The Test annotation supports two optional parameters. The first,
> expected, declares that a test method should throw an exception. If it
> doesn't throw an exception or if it throws a different exception than
> the one declared, the test fails.

答案2

得分: 0

将会抛出异常的调用之前,请放置 expectedException.expect

  1. @Test
  2. public void shouldThrowAnException_When_InputIsNull() {
  3. expectedException.expect(CalculationEngineException.class);
  4. expectedException.expectMessage("Error");
  5. calculatorBooking.calculate(null, null, 0, null);
  6. }
英文:

Place expectedException.expect before the call that will throw the exception.

  1. @Test
  2. public void shouldThrowAnException_When_InputIsNull() {
  3. expectedException.expect(CalculationEngineException.class);
  4. expectedException.expectMessage("Error");
  5. calculatorBooking.calculate(null, null, 0, null);
  6. }

答案3

得分: 0

你需要首先告诉JUnit该方法预计会抛出异常。然后,当异常被抛出时,它知道测试通过了。在你的代码中,你在异常被抛出后才放置了expect(),所以执行根本不会到那一步。正确的方式是:

  1. @Test
  2. public void shouldThrowAnException_When_InputIsNull() {
  3. expectedException.expect(CalculationEngineException.class);
  4. expectedException.expectMessage("Error");
  5. calculatorBooking.calculate(null, null, 0, null);
  6. }
英文:

You need to first tell JUnit that the method is expected to throw the exception. Then when it's thrown - it knows that the test passes. In your code you put expect() after the exception is thrown - so the execution doesn't even go that far. The right way:

  1. @Test
  2. public void shouldThrowAnException_When_InputIsNull() {
  3. expectedException.expect(CalculationEngineException.class);
  4. expectedException.expectMessage("Error");
  5. calculatorBooking.calculate(null, null, 0, null);
  6. }

huangapple
  • 本文由 发表于 2020年7月22日 20:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63034699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定