【发布时间】:2012-11-02 17:00:58
【问题描述】:
假设我有一个名为foo 的方法,对于某些输入值集,预期会成功完成并返回结果,而对于其他一些值集,预期会引发特定异常。此方法需要先进行一些设置,然后才能对其进行测试。
考虑到这些条件,最好将成功和失败测试集中在一个测试中,还是应该在单独的测试方法中维护这些案例?
换句话说,以下两种方法中哪一种更可取?
方法一:
@Test
public void testFoo() {
setUpThings();
// testing success case
assertEquals(foo(s), y);
// testing failure case
try {
foo(f);
fail("Expected an exception.")
} catch (FooException ex) {
}
}
方法2:
@Test
public void testFooSuccess() {
setUpThings();
assertEquals(foo(s), y);
}
@Test
public void testFooFailure() {
setUpThings();
try {
foo(f);
fail("Expected an exception.")
} catch (FooException ex) {
}
}
【问题讨论】:
标签: java unit-testing testing junit