断言异常被抛出

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

Assert an Exception is Thrown in

问题

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

public class CalculatorBookingTest {

    private CalculatorBooking calculatorBooking;

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Before
    public void setUp() {
        calculatorBooking = new CalculatorBooking();
    }

    @Test
    public void shouldThrowAnException_When_InputIsNull() {
        calculatorBooking.calculate(null, null, 0, null);
        expectedException.expect(CalculationEngineException.class);
        expectedException.expectMessage("Error");
    }

}

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

英文:

I have this Junit test in my project

public class CalculatorBookingTest {

    private CalculatorBooking calculatorBooking;

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Before
    public void setUp() {
        calculatorBooking = new CalculatorBooking();
    }

    @Test
    public void shouldThrowAnException_When_InputIsNull() {
        calculatorBooking.calculate(null, null, 0, null);
        expectedException.expect(CalculationEngineException.class);
        expectedException.expectMessage("Error");
    }

}

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

答案1

得分: 2

你可以这样做:

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

从 Junit 文档 中:

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

英文:

You could do something like:

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

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

@Test
public void shouldThrowAnException_When_InputIsNull() {
    expectedException.expect(CalculationEngineException.class);
    expectedException.expectMessage("Error");

    calculatorBooking.calculate(null, null, 0, null);
}
英文:

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

@Test
public void shouldThrowAnException_When_InputIsNull() {
    expectedException.expect(CalculationEngineException.class);
    expectedException.expectMessage("Error");

    calculatorBooking.calculate(null, null, 0, null);

}

答案3

得分: 0

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

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

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:

@Test
public void shouldThrowAnException_When_InputIsNull() {
    expectedException.expect(CalculationEngineException.class);
    expectedException.expectMessage("Error");

    calculatorBooking.calculate(null, null, 0, null);

}

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:

确定