【发布时间】:2011-05-02 03:21:55
【问题描述】:
这可能是一个概念上愚蠢的问题,但也可能不是,因为我还是一名学生,我想我应该没有问题。
假设您有一个方法,如果给定某些条件,它将抛出 NumberFormatException。我想编写一个单元测试来查看异常是否被正确解决。我怎样才能做到这一点?
附:我正在使用 JUnit 编写单元测试。
谢谢。
【问题讨论】:
标签: java unit-testing exception-handling junit
这可能是一个概念上愚蠢的问题,但也可能不是,因为我还是一名学生,我想我应该没有问题。
假设您有一个方法,如果给定某些条件,它将抛出 NumberFormatException。我想编写一个单元测试来查看异常是否被正确解决。我怎样才能做到这一点?
附:我正在使用 JUnit 编写单元测试。
谢谢。
【问题讨论】:
标签: java unit-testing exception-handling junit
使用@Test(expected=IOException.class)
http://junit.sourceforge.net/doc/faq/faq.htm#tests_7
如果您有一个预期的异常,这很好。另一种策略是在测试方法的末尾添加一个 Assert.fail() 。如果没有抛出异常,那么测试将相应地失败。例如
@Test
public void testIOExceptionThrown() {
ftp.write(); // will throw IOException
fail();
}
【讨论】:
正如其他海报所建议的,如果您使用的是 JUnit4,那么您可以使用注释:
@Test(expected=NumberFormatException.class);
但是,如果您使用的是旧版本的 JUnit,或者如果您想在同一个测试方法中执行多个“异常”断言,那么标准习惯用法是:
try {
formatNumber("notAnumber");
fail("Expected NumberFormatException");
catch(NumberFormatException e) {
// no-op (pass)
}
【讨论】:
假设您使用的是 JUnit 4,请在测试中以引发异常的方式调用该方法,并使用 JUnit 注释
@Test(expected = NumberFormatException.class)
如果抛出异常,则测试通过。
【讨论】:
@Test(expected=IndexOutOfBoundsException.class)
public void testIndexOutOfBoundsException() {
ArrayList emptyList = new ArrayList();
Object o = emptyList.get(0);
}
【讨论】:
在你的测试方法之前添加这个注解;它会成功的。
@Test(expected = java.lang.NumberFormatException.class)
public void testFooMethod() {
// Add code that will throw a NumberFormatException
}
【讨论】:
如果可以使用 JUnit 4.7,则可以使用 ExpectedException 规则
@RunWith(JUnit4.class)
public class FooTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void doStuffThrowsIndexOutOfBoundsException() {
Foo foo = new Foo();
exception.expect(IndexOutOfBoundsException.class);
exception.expectMessage("happened?");
exception.expectMessage(startsWith("What"));
foo.doStuff();
}
}
这比@Test(expected=IndexOutOfBoundsException.class) 好得多,因为如果在foo.doStuff() 之前抛出IndexOutOfBoundsException,测试将失败
【讨论】:
catch-exception 提供了一个不绑定到特定 JUnit 版本的解决方案,该解决方案旨在克服 JUnit 机制中固有的一些缺点。
【讨论】: