JUnit4 和异常处理

发布于 2024-09-16 06:15:03 字数 347 浏览 10 评论 0原文

由于无法访问 catch 块,以下代码无法编译。

我想编写“tues”函数并在许多其他单元测试函数中调用它。

这可能吗?如何实施?

private void catchException(boolean condition) {
        try
        {
            assertTrue(condition);
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getMessage());
        }
}

谢谢!

The following code does not compile, becouse of unreachable catch block.

I want write "tues" function and call it in many other unit test functions.

Is that possible and how to implement that?

private void catchException(boolean condition) {
        try
        {
            assertTrue(condition);
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getMessage());
        }
}

Thanks!

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

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

发布评论

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

评论(7

无法回应 2024-09-23 06:15:03

测试方法中无需捕获异常即可使其失败:

public Object methodUnderTest() throws SomeException {
    ...
}

@Test
public void testMethod() throws SomeException() {
      Object obj = methodUnderTest();
      assert...
}

如果 methodUnderTest() 抛出 SomeException,则 testMethod() 将失败。

There is zero need to catch an exception within a test method to fail it:

public Object methodUnderTest() throws SomeException {
    ...
}

@Test
public void testMethod() throws SomeException() {
      Object obj = methodUnderTest();
      assert...
}

If SomeException is thrown by methodUnderTest(), testMethod() will fail.

不念旧人 2024-09-23 06:15:03

您的代码的问题是 assertTrue 不会抛出 SomeException,如果您用抛出该异常的函数替换它,它将编译。

The problem with your code is that assertTrue does not throw SomeException, if you substitute it with the function that does, it will compile.

醉生梦死 2024-09-23 06:15:03

我不完全确定你在这里问什么,但你发布的代码有一些问题。

如果 try 中的代码可能会抛出与您正在捕获的类型相同的异常,则只能为受检查的异常使用 catch 块。由于检查异常必须在方法的 throws 子句中声明,因此您可以检查正在调用的任何方法是否抛出您正在捕获的异常类型。来自 上的 JUnit 文档assertTrue

public static void assertTrue(boolean condition)

可以看到它没有抛出任何检查异常。

此外,在 JUnit 测试中的 catch 块中调用 fail() 并不是真正必要的。如果抛出未捕获的异常,单元测试将自动失败。

I'm not entirely sure what you're asking here, but there are a couple things wrong with the code you posted.

You can only have a catch block for a checked exception if the code in the try can potentially throw an exception of the same type you are catching. Since checked exceptions must be declared in the throws clause of a method, you can check to see if any of the methods you are calling throw the exception types you are catching. From the JUnit documentation on assertTrue:

public static void assertTrue(boolean condition)

You can see it doesn't throw any checked exceptions.

Also, calling fail() in a catch block in a JUnit test is not really necessary. A unit test will automatically fail if an uncaught exception is thrown.

我们的影子 2024-09-23 06:15:03

只需将 throws WhateverException 添加到您的测试签名中,如果抛出异常,测试就会失败。

Just add throws WhateverException to your test signature and the test will fail if an exception is thrown.

☆獨立☆ 2024-09-23 06:15:03

实际上并不清楚您要通过代码实现什么目的。如果您希望在断言失败时在 jUnit 引发的异常中拥有格式良好的消息,请考虑以这种方式编写它:

assertTrue("Condition should hold because....", conditionToCheck);

这样,如果检查失败,jUnit 将打印您提供的消息。我强烈建议这样做,特别是如果您有大量测试,因为它

  • 可以帮助您快速识别问题
  • ,帮助您的团队成员理解您断言的目的

It is actually not really clear what you what to achieve with your code. If you want to have a nicely formatted message in the exception fired by jUnit if your assert fails then consider writing it in this way:

assertTrue("Condition should hold because....", conditionToCheck);

In this way jUnit will print the message you provided if the check fails. I highly recommend this, especially if you have lots of tests because it

  • helps you to quickly identify the problem
  • helps your team member to understand the purpose of your assert
離殇 2024-09-23 06:15:03

通过 Exception 修改 SomeException 将导致代码编译。

Modifying SomeException by Exception will cause the code to compile.

剪不断理还乱 2024-09-23 06:15:03

显然我的问题不清楚。

我有很多单元测试,所有方法都抛出相同的异常和不同的错误消息。 “SomeException”是异常,我必须捕获并从中读取错误消息。

我想要实现的是编写一种方法,该方法对于所有单元测试都是通用的,并且可以在其中打印错误消息。

现在单元测试看起来像这样

 public void test_something()
    {     
        try
        {
            assertTrue(bw.doSomething("test"));
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getReason());
        }
    }

public void test_something1()
    {
        IBroadworks bw = getSomehting1();
        try
        {
            assertTrue(bw.doSomething1("test1"));
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getReason());
        }
    }
...

所以下面的代码在所有单元测试中重复,这是不行的。

...
try{
assertTrue(condition);
}

catch (SomeException e)
        {
            fail("exception = " + e.getReason());
        }
...

Obviously my question is not clear.

I have many unit tests and all methods are throwing same exception with different error message. "SomeException" is exception I must catch and read error message from it.

What I want to achive is to write one method which will be common to all unit tests and where I could print error message.

Now unit tests looks like this

 public void test_something()
    {     
        try
        {
            assertTrue(bw.doSomething("test"));
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getReason());
        }
    }

public void test_something1()
    {
        IBroadworks bw = getSomehting1();
        try
        {
            assertTrue(bw.doSomething1("test1"));
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getReason());
        }
    }
...

So below code is repeating in all unit tests and that is not ok.

...
try{
assertTrue(condition);
}

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