23

Possibly this might be already obvious in the JUnit docs, but can't seem to find it, nor remember if there is a solution for what I'm about to describe.

When I'm running my test cases via maven (mvn test), stack traces of unexpected exceptions are not shown in standard error at all. All I get is a message indicating that a test failed.

If my code under test throws an unchecked exception, say a NPE, my test case output looks like this:

Tests in error:
  testFoo(bar.Foo)

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

If my test case declares it can throw an Exception object, and I wrap the run-time exception like this:

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

Then we get this:

Tests in error:
  testFoo(bar.Foo): java.lang.NullPointerException

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

Now I'm getting something barely useful. However, I would like to see the full enchilada, the entire stack trace. But I don't seem to find a way to do so with JUnit other than kludging my test cases within a catch/try block to print the stack trace to 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 is manual, and I have to do it for every test case (I rather put the elbow grease and do it so that I get a stack trace right there and there, than to need a stack trace and not have it.)

Is there a way to utilize/tell JUnit so that it prints the stack trace of any uncaught exception without *having to rely on explicitly catching/printing stack traces within my test case code*?

Thanks.

luis.espinal
  • 10,331
  • 6
  • 39
  • 55
  • 1
    Do you want your test to PASS when an exception occurs, or to FAIL? Why do you want to print the whole stacktrace - I suppose for debugging - but for this you can see the full stacktrace in your IDE. – Honza Zidek Mar 21 '14 at 09:18
  • 2
    True that I could with the IDE. I guess it just personal preference to get the stack trace in the build logs. Stack traces are the things I do not mind having a lot in my logs :) – luis.espinal Mar 21 '14 at 12:15
  • Did you check the test run log in the surefire-reports directory (or something like that, under the target dir)? What prints in the main Maven log is a summary. I think the details are in the reports directory. – user944849 Mar 21 '14 at 13:52
  • @luis.espinal: Sorry for being nit-picking :-) You wrote "Stack traces are the things I do not mind having a lot in my logs". Is it REALLY what you want? Stack trace is something you need to reproduce/fix the bug, not for utilizing your unused disk space :) Anyway you have to re-run the failed test in your IDE and it's even more comfortable to jump directly to the culprit code. Why bother with something you still have not persuaded me about that you really do NEED? :) – Honza Zidek Mar 21 '14 at 15:34
  • 1
    @user944849 - I do. It's just that I prefer to see my everything in one place (the build log) as opposed to looking through different logs per test case ;) – luis.espinal Mar 21 '14 at 18:59
  • 1
    @HonzaZidek - Hi Honza. Disk space is cheap, and it is not like build logs are kept forever - we shouldn't be keeping build logs per branch for more than a few days (subjective I know.) See, for one, it is not that we are expecting every single test to fail, and also, if we are running out of disk space due to keeping the stack trace for the instances when they occur, I think we have more profound problems in terms of build infrastructure to worry about ;) – luis.espinal Mar 21 '14 at 19:01
  • I guess, it is just a personal way of working. True, I can run the tests on the IDE. True that I can navigate under the surefire plugin directory logs and go through the individual logs. And I'm sure a lot of people are successful at doing that. For me, I can make it work, but I truly hate it, and I rather have the stack traces on the build logs just as I would have them on a well-produced application log. – luis.espinal Mar 21 '14 at 19:03

1 Answers1

14

Try setting the maven-surefire-plugin's useFile parameter to false. Per the Surefire plugin docs this outputs the test reports to the console.

Also see the trimStackTrace parameter.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • 1
    More details regarding the solution : https://stackoverflow.com/questions/2928548/make-mavens-surefire-show-stacktrace-in-console – epol Jan 31 '20 at 14:09