【发布时间】:2012-12-06 19:36:35
【问题描述】:
我最近尝试了 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]'>
....
除了 yesDataPoint @DataPoint 导致失败之外,有没有办法摆脱对 *my*test 没有任何说明的 24 行?这是我需要的信息,以了解失败的原因,但我真的很想知道如何同时失败。
[编辑]
我用经典的 org.junit.Assert.assertEquals 替换了 org.fest.assertions 的用法,以避免混淆。 此外,它也与 Eclipse 无关:当您从命令行运行 @Theory 并使其失败时,您也会得到那个长(无用/令人困惑的)堆栈跟踪。
【问题讨论】:
-
你是如何在“对源代码的反应”之前得到这 23 行的?在 Eclipse 中从故障跟踪视图中获取它们,我确实转到了您正在寻找的“源代码反应”,“复制跟踪”,将其插入某处,然后我才能看到所有这些行。
-
@Gangnus Eclipse 过滤掉了很多堆栈跟踪。查看 Workspace->Preferences->Java->JUnit。这就是为什么您在 Eclipse 中看不到它们的原因。
-
对不起我的英语。我已经把它们过滤掉了。我所说的,我需要做一些事情才能看到这些额外的线条。不过谢谢你的建议。
标签: java unit-testing testing junit junit-theory