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.
You have a very strange library. You have a strange syntax for assertThat. I would propose:
Then you'll have:
As for assertEqual you are right, it seems it won't help in Theories. Only for the @Test:
An addition:
You can also use
Than you'll have the output:
As for filtering extra lines, use Failure Trace View in Eclipse.
Is there a problem with catching the ComparisonFailure and printing the GetMessage() of it?
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.