Force JUnit to display invisible stack traces

Perhaps this may already be obvious in the JUnit docs, but it doesn't seem to be able to find it, and don’t remember if there is a solution for what I am going to describe.

When I run my test cases via maven ( mvn test ), the trace stacks of unexpected exceptions do not appear at all in the standard error. All I get is a message indicating that the test failed.

If my test code throws an exception, such as NPE, the output of the test case looks like this:

 Tests in error: testFoo(bar.Foo) Tests run: 1, Failures: 0, Errors: 1, Skipped: 0 

If my test case states that it can throw an Exception object, and I will end the runtime exception as follows:

 @Test public void testFoo() throws Exception { try{ // something funky throw new NullPointerException(); } catch( RuntimeException ex ) { throw new Exception(ex); } } 

Then we get:

 Tests in error: testFoo(bar.Foo): java.lang.NullPointerException Tests run: 1, Failures: 0, Errors: 1, Skipped: 0 

Now I get something of little use. However, I would like to see the complete enchilada, the entire stack trace. But I didn't seem to find a way to do this with JUnit, except that I removed my test cases in a catch / try block to print a stack trace with a standard error (ugly):

 @Test public void testFoo() throws Exception { try{ // something funky throw new NullPointerException(); } catch( RuntimeException ex ) { ex.printStackTrace(); // << this works, but it is ugly. throw ex; } } 

I hate doing this because it pollutes (IMO) my test case logic; and it needs to be done manually, and I have to do it for each test case (I would rather put my elbow grease on and do it so that I get the stack trace right there and there, than I need the stack trace and not have it.)

Is there a way to use / tell JUnit to print the stack trace of any uncaught exception without having to rely on explicitly traceable stack traces in my validation * code?

Thanks.

+7
java maven unit-testing junit
source share
1 answer

Try setting maven-surefire-plugin useFile to false. In Documents Plugin, Surefire displays reports on tests on the console.

Also see trimStackTrace parameter.

+5
source share

All Articles