tekumara
tekumara

Reputation: 8817

ScalaTest display original Failure stacktrace

How can display the original stacktrace for a Try Failure as part of the test output?

When I do the following:

 result.success.value should equal blah

And the result is a Failure I get

The Try on which success was invoked was not a Success.

Or, if I do this first:

result should be a 'success

It's a little more informative, because I can see the exception:

Failure(java.lang.IllegalArgumentException: Cannot format given Object as a Date) was not a success

But the stack trace shows where it failed in the test, not the original stack trace of the Failure.

Upvotes: 0

Views: 791

Answers (1)

Ivan Klass
Ivan Klass

Reputation: 6627

If some error-causing code is wrapped into Try, it means that exception is handled somewhere inside calculation, and it's cause and message will not be printed somewhere until explicitly requested. In order to see original cause, you can access failure object directly (docs), or handle failure manually:

val (ok, message) = result match {
  case Success(v) => (true, "")
  case Failure(ex) =>
    (false, ex.getMessage + "\n" + ex.getStackTrace.mkString("\n"))
}

assert(ok, message)

This link also looks helpful.

If you want original stack trace to be printed - use result.get, but check not for 'success but inner type/value:

result.get should equal blah

Upvotes: 3

Related Questions