JUnit测试抛出异常的错误形式? [英] Bad form for JUnit test to throw exception?

查看:193
本文介绍了JUnit测试抛出异常的错误形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JUnit很陌生,我真的不知道异常和异常处理的最佳实践。

I'm pretty new to JUnit, and I don't really know what best practices are for exceptions and exception handling.

例如,假设我说'我为IPAddress类编写测试。它有一个构造函数IPAddress(String addr),如果addr为null,它将抛出InvalidIPAddressException。据我在google搜索时可以看出,null参数的测试将如下所示。

For example, let's say I'm writing tests for an IPAddress class. It has a constructor IPAddress(String addr) that will throw an InvalidIPAddressException if addr is null. As far as I can tell from googling around, the test for the null parameter will look like this.

@Test
public void testNullParameter()
{
    try
    {
        IPAddress addr = new IPAddress(null);
        assertTrue(addr.getOctets() == null);
    }
    catch(InvalidIPAddressException e)
    {
        return;
    }

    fail("InvalidIPAddressException not thrown.");
}

在这种情况下,try / catch有意义,因为我知道异常即将到来。

In this case, try/catch makes sense because I know the exception is coming.

但是现在如果我想写testValidIPAddress(),有几种方法可以做到:

But now if I want to write testValidIPAddress(), there's a couple of ways to do it:

方式#1:

@Test
public void testValidIPAddress() throws InvalidIPAddressException
{
    IPAddress addr = new IPAddress("127.0.0.1");
    byte[] octets = addr.getOctets();

    assertTrue(octets[0] == 127);
    assertTrue(octets[1] == 0);
    assertTrue(octets[2] == 0);
    assertTrue(octets[3] == 1);
}

方式#2:

@Test
public void testValidIPAddress()
{
    try
    {
        IPAddress addr = new IPAddress("127.0.0.1");
        byte[] octets = addr.getOctets();

        assertTrue(octets[0] == 127);
        assertTrue(octets[1] == 0);
        assertTrue(octets[2] == 0);
        assertTrue(octets[3] == 1);
    }
    catch (InvalidIPAddressException e)
    {
        fail("InvalidIPAddressException: " + e.getMessage());
    }
}

标准做法是向JUnit抛出意外的异常或只是自己处理它们?

Is is standard practice to throw unexpected exceptions to JUnit or just deal with them yourself?

感谢您的帮助。

推荐答案

实际上,旧样式的异常测试是围绕抛出异常的代码包装try块,然后添加 fail()语句在try块的末尾。这样的事情:

Actually, the old style of exception testing is to wrap a try block around the code that throws the exception and then add a fail() statement at the end of the try block. Something like this:

public void testNullParameter() {
    try {
        IPAddress addr = new IPAddress(null);
        fail("InvalidIPAddressException not thrown.");
    } catch(InvalidIPAddressException e) {
        assertNotNull(e.getMessage());
    }
}

这和你写的没什么不同但是:

This isn't much different from what you wrote but:


  1. 您的 assertTrue(addr.getOc​​tets()== null); 是无用。

  2. 意图和语法更清晰,更容易阅读。

  1. Your assertTrue(addr.getOctets() == null); is useless.
  2. The intend and the syntax are clearer IMO and thus easier to read.

不过,这有点难看。但这就是JUnit 4拯救的地方,因为异常测试是JUnit 4中最大的改进之一。使用JUnit 4,你现在可以编写你的测试:

Still, this is a bit ugly. But this is where JUnit 4 comes to the rescue as exception testing is one of the biggest improvements in JUnit 4. With JUnit 4, you can now write your test like this:

@Test (expected=InvalidIPAddressException.class) 
public void testNullParameter() throws InvalidIPAddressException {
    IPAddress addr = new IPAddress(null);
}

很好,不是吗?

现在,关于真正的问题,如果我不期望抛出异常,我肯定会选择#1(因为它不那么详细)并且让JUnit处理异常并且无法通过测试正如所料。

Now, regarding the real question, if I don't expect an exception to be thrown, I'd definitely go for way #1 (because it's less verbose) and let JUnit handle the exception and fail the test as expected.

这篇关于JUnit测试抛出异常的错误形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆