3

The scala test code:

import play.api.test._
import scala._
import org.specs2.execute.Result

object ThrowTest extends PlaySpecification {

  "throwA" should {
    "catch the exception test1" in {
      world must throwA[Exception]
    }
    "catch the exception test2" in {
      hello {
        world =>
          world must throwA[Exception]
      }
    }
  }

  def hello(action: (String) => Result) = {
    action(world)
  }

  def world: String = {
    throw new Exception("world-exception")
  }

}

Why test1 is working as I expected, but test2 is not, which throws the exception to outer and never catch it:

[info] ! catch the exception test2
[error]     Exception: world-exception (ThrowTest.scala:26)
[error] database.ThrowTest$.world(ThrowTest.scala:26)
[error] database.ThrowTest$.hello(ThrowTest.scala:22)
[error] database.ThrowTest$$anonfun$1$$anonfun$apply$4.apply(ThrowTest.scala:14)
[error] database.ThrowTest$$anonfun$1$$anonfun$apply$4.apply(ThrowTest.scala:14)
[info] Total for specification ThrowTest
Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

2

Because for test 2 your exception is thrown from hello before calling action. action is a String => Result and you call it with world which - when evaluated - throws an exception, therefor, all this code:

world =>world must throwA[Exception]

is never executed.

vptheron
  • 7,426
  • 25
  • 34
  • just to make it a little bit explicit, what you (@Freewind) have in your code in hello function is: `val result = world; action(result)` – om-nom-nom Mar 31 '14 at 12:56