我最近尝试了 Junit @Theory 测试风格:这是一种非常有效的测试方式。但是,我对测试失败时引发的异常不满意。例子 :
import static org.junit.Assert.assertEquals;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class TheoryAndExceptionTest {
@DataPoint
public static String yesDataPoint = "yes";
@Theory
public void sayNo(final String say) {
assertEquals("no",say);
}
}
我希望这个测试抛出一个描述性异常,而不是得到类似的东西:
org.junit.ComparisonFailure: expected:<'[no]'> but was:<'[yes]'>
...我明白了:
org.junit.experimental.theories.internal.ParameterizedAssertionError: sayNo(yes) at
....
[23 lines of useless stack trace cut]
...
Caused by: org.junit.ComparisonFailure: expected:<'[no]'> but was:<'[yes]'>
....
有没有办法摆脱前 24 行,除了 yesDataPoint @DataPoint 导致失败之外什么都不告诉 * my *test ?这是我需要的信息,以了解失败的原因,但我真的很想知道它是如何同时失败的。
[编辑]
我用经典的 org.junit.Assert.assertEquals 替换了 org.fest.assertions 的用法,以避免混淆。此外,它也与 Eclipse 无关:当您从命令行运行 @Theory 并使其失败时,您也会得到那个长(无用/令人困惑的)堆栈跟踪。