Junit异常处理

发布于 2024-11-03 17:30:35 字数 382 浏览 3 评论 0原文

我想知道这个测试用例应该通过还是失败 因为 预期 = IndexOutOfBoundsException.class 实际上它正在抛出算术异常。谁能解释一下吗?

@Test(expected = IndexOutOfBoundsException.class)
public void testDivideNumbers()throws ArithmeticException{
    try{
        double a = 10/0;
        fail("Failed: Should get an Arithmatic Exception"); 

        }
    catch (ArithmeticException e) {

        }   

}

I want to know that whether this test case should pass or fail
beacause
expected = IndexOutOfBoundsException.class
and actually it is throwing Arithmatic exception. can anyone explain?

@Test(expected = IndexOutOfBoundsException.class)
public void testDivideNumbers()throws ArithmeticException{
    try{
        double a = 10/0;
        fail("Failed: Should get an Arithmatic Exception"); 

        }
    catch (ArithmeticException e) {

        }   

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尘曦 2024-11-10 17:30:35

要测试是否抛出正确的异常,您不应让测试方法抛出异常,而应让测试本身导致抛出的异常。

因此,如果预期出现 ArithmeticException,则测试应该是:

@Test(expected = ArithmeticException.class)
public void testDivideNumbers() {
     double a = 10/0;
}

To test that the correct exception is thrown you should not have the test method throw the exception but just have the test itself result in the thrown exception.

So if ArithmeticException is expected then the test should be:

@Test(expected = ArithmeticException.class)
public void testDivideNumbers() {
     double a = 10/0;
}
山人契 2024-11-10 17:30:35

它应该失败,因为它不会抛出任何异常; ArithmeticException 被 catch 块捕获并吞掉。

It should fail because it doesn't throw any exception; the ArithmeticException is caught and swallowed by the catch block.

著墨染雨君画夕 2024-11-10 17:30:35

此测试预计会抛出 IndexOutOfBoundsException。因为测试中没有发生这种情况,所以测试失败。您可以像这样“修复”测试:

@Test(expected = IndexOutOfBoundsException.class)
public void testDivideNumbers() {
    try {
        double a = 10/0;
        Assert.fail("Failed: Should get an Arithmetic Exception"); 
    }
    catch (ArithmeticException e) {
        // Assert that this exception is thrown as expected
        Assert.assertEquals("/ by zero", e.getMessage());
    }
    throw new IndexOutOfBoundsException();
}

您不应将 catch 块留空。您应该始终在其中添加一些断言,以证明失败()没有发生并且捕获确实发生,并且重要的是,由于您期望的原因而发生。

This test is expecting to get an IndexOutOfBoundsException thrown. Because that does not happen in the test, the test fails. You can "fix" the test like this:

@Test(expected = IndexOutOfBoundsException.class)
public void testDivideNumbers() {
    try {
        double a = 10/0;
        Assert.fail("Failed: Should get an Arithmetic Exception"); 
    }
    catch (ArithmeticException e) {
        // Assert that this exception is thrown as expected
        Assert.assertEquals("/ by zero", e.getMessage());
    }
    throw new IndexOutOfBoundsException();
}

You should not leave the catch block empty. You should always put some assert in it proving that the fail() didn't happen and the catch did happen and, importantly, happened for the reason you expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文