即使我有预期的注释,JUnit 测试在 eclipse 中运行也会返回错误

发布于 2024-08-13 08:22:33 字数 2627 浏览 6 评论 0原文

(这是这个问题的后续问题。)

我在 Eclipse 中进行 JUnit4 测试时遇到问题。我正在尝试使用 @Test(expected=...) 注释来单元测试异常抛出。问题是,当我运行测试时,它们会作为错误返回而不是通过,即使我期望抛出异常。

我正在使用 eclipse 3.4.0 和 JUnit 4.3.1。

这是代码:

要测试的类:

public class IPAddress
{
    private byte[] octets;

    private IPAddress()
    {
        octets = new byte[4];
    }

    public IPAddress(String addr) throws InvalidIPAddressException
    {
        this();

        if(addr == null || !isValid(addr))
            throw new InvalidIPAddressException(addr);

        String strOctets[] = addr.split("\\.");

        for (int i = 0; i < strOctets.length; i++)
            octets[i] = Byte.parseByte(strOctets[i]);
    }

    public static boolean isValid(String addr)
    {
        String strOctets[] = addr.split("\\.");

        if (strOctets.length != 4)
            return false;

        for (int i = 0; i < strOctets.length; i++)
        {
            try
            {
                int num = Integer.parseInt(strOctets[i]);

                if (num < 0 || num > 255)
                    return false;
            } catch (NumberFormatException e)
            {
                return false;
            }
        }

        return true;
    }

    public byte[] getOctets()
    {
        return octets;
    }
}

异常:

public class InvalidIPAddressException extends Exception
{
    public InvalidIPAddressException(String addr)
    {
        super("\"" + addr + "\" is not a valid IP address");
    }
}

测试用例:

public class IPAddressTest extends TestCase
{
    @Test(expected=InvalidIPAddressException.class)
    public void testNullParameter() throws InvalidIPAddressException
    {
        @SuppressWarnings("unused")
        IPAddress addr = new IPAddress(null);
        fail("InvalidIPAddressException not thrown.");
    }

    @Test(expected=InvalidIPAddressException.class)
    public void testHostnameParameter() throws InvalidIPAddressException
    {
        @SuppressWarnings("unused")
        IPAddress addr = new IPAddress("http://www.google.com");
        fail("InvalidIPAddressException not thrown.");
    }

    @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);
    }

    public static void main(String[] args)
    {
        TestRunner.run(IPAddressTest.class);
    }
}

(This is a follow up question to this one.)

I'm having a problem with JUnit4 tests in eclipse. I'm trying to use the @Test(expected=...) annotation to unit test exception throwing. The problem is that when I run the tests, they come back as errors instead of passing, even though I'm expecting the exceptions to be thrown.

I'm using eclipse 3.4.0 and JUnit 4.3.1.

Here's the code:

Class to test:

public class IPAddress
{
    private byte[] octets;

    private IPAddress()
    {
        octets = new byte[4];
    }

    public IPAddress(String addr) throws InvalidIPAddressException
    {
        this();

        if(addr == null || !isValid(addr))
            throw new InvalidIPAddressException(addr);

        String strOctets[] = addr.split("\\.");

        for (int i = 0; i < strOctets.length; i++)
            octets[i] = Byte.parseByte(strOctets[i]);
    }

    public static boolean isValid(String addr)
    {
        String strOctets[] = addr.split("\\.");

        if (strOctets.length != 4)
            return false;

        for (int i = 0; i < strOctets.length; i++)
        {
            try
            {
                int num = Integer.parseInt(strOctets[i]);

                if (num < 0 || num > 255)
                    return false;
            } catch (NumberFormatException e)
            {
                return false;
            }
        }

        return true;
    }

    public byte[] getOctets()
    {
        return octets;
    }
}

Exception:

public class InvalidIPAddressException extends Exception
{
    public InvalidIPAddressException(String addr)
    {
        super("\"" + addr + "\" is not a valid IP address");
    }
}

Test case:

public class IPAddressTest extends TestCase
{
    @Test(expected=InvalidIPAddressException.class)
    public void testNullParameter() throws InvalidIPAddressException
    {
        @SuppressWarnings("unused")
        IPAddress addr = new IPAddress(null);
        fail("InvalidIPAddressException not thrown.");
    }

    @Test(expected=InvalidIPAddressException.class)
    public void testHostnameParameter() throws InvalidIPAddressException
    {
        @SuppressWarnings("unused")
        IPAddress addr = new IPAddress("http://www.google.com");
        fail("InvalidIPAddressException not thrown.");
    }

    @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);
    }

    public static void main(String[] args)
    {
        TestRunner.run(IPAddressTest.class);
    }
}

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

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

发布评论

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

评论(2

苏佲洛 2024-08-20 08:22:33

看来您混淆了 JUnit 3 和 4 代码。 TestRunner 是 JUnit 3 的一部分,因此根本不查看注释。

尝试删除 extends TestCase 并将 TestRunner.run(IPAddressTest.class); 更改为 JUnitCore.runClasses(IPAddressTest.class); (您将需要根据需要添加导入)。

It looks like you're mixing up JUnit 3 and 4 code. TestRunner is part of JUnit 3, so doesn't look at the annotations at all.

Try removing extends TestCase and changing TestRunner.run(IPAddressTest.class); to JUnitCore.runClasses(IPAddressTest.class); (you'll need to add the imports as required).

撩人痒 2024-08-20 08:22:33

可能是因为当您尝试实例化 InvalidIPAddressException 时,构造函数在尝试连接其中的字符串时会抛出 NullPointerException。

public InvalidIPAddressException(String addr)
{
    super("\"" + addr + "\" is not a valid IP address"); // NPE while concatenate
}

Likely because when you try to instantiate the InvalidIPAddressException the constructor throw a NullPointerException when it tries to concatenate the string in it.

public InvalidIPAddressException(String addr)
{
    super("\"" + addr + "\" is not a valid IP address"); // NPE while concatenate
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文