即使我有预期的注释,JUnit 测试在 eclipse 中运行也会返回错误
(这是这个问题的后续问题。)
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您混淆了 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 changingTestRunner.run(IPAddressTest.class);
toJUnitCore.runClasses(IPAddressTest.class);
(you'll need to add the imports as required).可能是因为当您尝试实例化 InvalidIPAddressException 时,构造函数在尝试连接其中的字符串时会抛出 NullPointerException。
Likely because when you try to instantiate the InvalidIPAddressException the constructor throw a NullPointerException when it tries to concatenate the string in it.