JUnit @Theory : is there a way to throw meaningful exception?

五迷三道 提交于 2019-12-24 02:52:07

问题


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]'>

... I get this :

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]'>
....

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.

[edited]

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.


回答1:


Is there a problem with catching the ComparisonFailure and printing the GetMessage() of it?

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

Apologies if there is something I misunderstand.

Edit: ComparisonFailure also has getExpected() and getActual() methods that you can invoke if you are looking for certain formatting.




回答2:


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");
    }
}

Then you'll have:

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

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

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"));

Than you'll have the output:

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

As for filtering extra lines, use Failure Trace View in Eclipse.



来源:https://stackoverflow.com/questions/13751161/junit-theory-is-there-a-way-to-throw-meaningful-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!