How do I print a Groovy stack trace?

I found this questions when searching for "spock print full stack trace".

My unit tests are written in Groovy, using the Spock testing framework and they're run in the context of a Gradle build.

The fix for me was as simple as adding exceptionFormat = 'full' to my Gradle test task specification:

test {
  testLogging {
    exceptionFormat = 'full'
  }
}

Solution:

org.codehaus.groovy.runtime.StackTraceUtils.sanitize(new Exception()).printStackTrace()

Original answer:

A Google search returns the following information:

Apparently, there is a method in org.codehaus.groovy.runtime.StackTraceUtils called printSanitizedStackTrace. There isn't much documentation for the method, though there is a method called sanitize which is described as

remove all apparently groovy-internal trace entries from the exception instance This modifies the original instance and returns it, it does not clone

So I would try org.codehaus.groovy.runtime.StackTraceUtils.printSanitizedStackTrace(Throwable t) (it is static) and see if that works for you.

Tags:

Groovy