JUnit @Theory:有没有办法抛出有意义的异常?

2023-12-22

我最近尝试了 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 行,这些行没有任何关于 * 的信息my*测试,除了 yesDataPoint @DataPoint 导致失败?这是我需要的信息,以了解失败的原因,但我真的很想知道how它同时失败了。

[edited]

我用经典的 org.junit.Assert.assertEquals 替换了 org.fest.assertions 用法,以避免混淆。 此外,它也与 Eclipse 无关:当您从命令行运行 @Theory 并失败时,您也会得到很长(无用/令人困惑)的堆栈跟踪。


捕捉有问题吗比较失败 http://kentbeck.github.com/junit/javadoc/latest/org/junit/ComparisonFailure.html并打印它的 GetMessage() ?

public void sayNo(final String say) {
    try {
        assertThat(say).isEqualTo("no");
    }catch(ComparisonFailure e) {
        System.out.println(e.getMessage);
    }
}

如果我有什么误解,请道歉。

编辑:ComparisonFailure 还具有 getExpected() 和 getActual() 方法,如果您正在查找某些格式,则可以调用它们。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JUnit @Theory:有没有办法抛出有意义的异常? 的相关文章

随机推荐