【问题标题】:JUnit @Theory : is there a way to throw meaningful exception?JUnit @Theory:有没有办法抛出有意义的异常?
【发布时间】: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


【解决方案1】:

捕获ComparisonFailure 并打印它的GetMessage() 是否有问题?

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

如果有什么我误解的地方,请见谅。

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

【讨论】:

  • 在测试中捕获异常真的很糟糕......而且当你对 System.out.println 失败的原因执行它时更糟;)
  • @Oliver 好的,抱歉。只是想在这里学习。不是所有未捕获的异常都会打印堆栈跟踪吗? (我的无知是因为刚刚学习单元测试)
【解决方案2】:

你有一个非常奇怪的图书馆。 assertThat 的语法很奇怪。我建议:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.experimental.theories.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

@RunWith(Theories.class)
public class TheoryAndExceptionTest {

    @DataPoint
    public static String yesDataPoint = "yes";

    @Theory
    public void sayNo(final String say) {

        assertThat(say,is("no"));
    }

    @Test
    public void yesNo() {
        assertEquals("must be equal, ", "yes","no");
    }
}

那么你将拥有:

org.junit.experimental.theories.internal.ParameterizedAssertionError: sayNo(yesDataPoint)
....

Caused by: java.lang.AssertionError: 
Expected: is "no"
     got: "yes"

至于 assertEqual 你是对的,它似乎对理论没有帮助。 仅适用于@Test:

    org.junit.ComparisonFailure: must be equal,  expected:<[yes]> but was:<[no]>

补充:

你也可以使用

assertThat("must be equal, ", say,is("no"));

比你有输出:

Caused by: java.lang.AssertionError: must be equal, 
Expected: is "no"
     got: "yes"

至于过滤多余的行,使用Eclipse中的Failure Trace View。

【讨论】:

  • Fest assert 不是问题:使用 assertEqual(msg, expected, actual) 只会改变根本原因(原因:junit.framework.ComparisonFailure: ... 而不是 org.junit.ComparisonFailure)
  • assertThat 的奇怪语法来自 Fluent 断言 (bit.ly/MRtJpl),你应该试试看 :)
  • 我的 Juno Eclipse 拒绝理解您的代码。请在此处显示您使用的所有进口产品。
  • 你有没有注意到,org.fest.assertions 不属于 junit (github.com/alexruiz/fest-assert-2.x/wiki/…)?它可以被 JUnit 使用,但不属于它。试试 JUnit assertThat。
  • 我已经从 org.fest 尝试过你的 assertThat。这是指定的问题 - 与来自 JUnit 的 assertThat 和 assertEquals 相比,来自 org.fest 的 assertThat 确实没有在故障跟踪中提供明显必要的信息
猜你喜欢
  • 2015-07-11
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
相关资源
最近更新 更多