Search code examples
mavenjunit

Why does maven/junit only print the "bottommost" line of a stack trade when an exception is thrown?


I'm seeing some odd behavior in the output of failed tests when using JUnit with maven. Specifically, the stack trace printed for a RuntimeException only has the "bottommost"/deepest line printed.

For example:

[INFO] Running com.foo.MyTest
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.59 s <<< FAILURE! - in com.foo.MyTest
[ERROR] testBar(com.foo.MyTest)  Time elapsed: 0.588 s  <<< ERROR!
java.lang.NullPointerException
        at com.foo.MyTest.testBar(MyTest.java:132)

The problem is that the NPE doesn't occur on line 132 of MyTest.java. There, I call something, which calls something else, and so on, and about 10 method calls later is my NPE. It's almost if they take the whole stack trace and throw out everything except the first method call in it. This makes it impossible to debug where the actual exception is being thrown of course.

I am running the tests with the command mvn package assembly:single. Running with the -e flag ("To see the full stack trace of the errors") only prints "internal" stack traces like the following without any of my code:

at org.apache.maven.plugin.surefire.SurefireHelper.throwException (SurefireHelper.java:289)
at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution (SurefireHelper.java:161)
at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary (SurefirePlugin.java:364)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked (AbstractSurefireMojo.java:1041)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute (AbstractSurefireMojo.java:857)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)

Is something misconfigured somewhere? Happy to provide more info to debug if it's necessary.

Thanks.


Solution

  • There is a configuration <trimStackTrace>false</trimStackTrace> https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#trimStackTrace which is by default true