JUnit @Theory:有没有一种方法可以引发有意义的异常? [英] JUnit @Theory : is there a way to throw meaningful exception?
问题描述
我最近尝试了Junit @Theory测试样式:这是一种非常有效的测试方法.但是,我对测试失败时引发的异常感到不满意.例子:
I tried Junit @Theory test style recently : it's a really efficient way of testing. However, i not pleased with the exception that are thrown when a test fails. Example :
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);
}
}
我希望此测试会引发描述性异常,但不会出现类似以下内容的情况:
I expect this test to throw a descriptive exception, but instead of getting something like :
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 * test无效的第一行,除了yesDataPoint @DataPoint会导致失败之外?这是我需要的信息,以了解失败的原因,但是我真的很想知道如何同时失败.
Is there a way to get rid of the 24 first lines that tell nothing about *my*test, except that yesDataPoint @DataPoint causes the failure ? That's an information i need, to know what is failing, but i really would like to know how it fails on the same time.
我将org.fest.assertions用法替换为经典的org.junit.Assert.assertEquals,以避免造成混淆. 此外,它与Eclipse也没有关系:长的(无用/混乱)堆栈跟踪也是在命令行中运行@Theory并使其失败时得到的.
I replaced org.fest.assertions usage by classic org.junit.Assert.assertEquals, to avoid confusion. Additionally, it's not related either with Eclipse : that long (useless/confusing) stack trace is what you get too when you run and fail a @Theory from the command line.
推荐答案
您有一个非常奇怪的库.您对assertThat有一种奇怪的语法.我会建议:
You have a very strange library. You have a strange syntax for assertThat. I would propose:
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:
As for assertEqual you are right, it seems it won't help in Theories. Only for the @Test:
org.junit.ComparisonFailure: must be equal, expected:<[yes]> but was:<[no]>
附加项:
您也可以使用
An addition:
You can also use
assertThat("must be equal, ", say,is("no"));
比您将得到的输出:
Caused by: java.lang.AssertionError: must be equal,
Expected: is "no"
got: "yes"
关于过滤多余的行,请使用Eclipse中的Failure Trace View.
As for filtering extra lines, use Failure Trace View in Eclipse.
这篇关于JUnit @Theory:有没有一种方法可以引发有意义的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!