score:-1

You could set up a callback on the mock:

  val foo = mock[Foo]
  foo.bar(anyString) answers (derp => {
    derp match {
      case "x" => 1
      case "y" => 2
      case _ => throw mock[FooException]
    }
  })

There are some more details in the specs2 documentation.

score:0

You need to change the order of your stubbing around. Stub the generalized behavior first (anyString) and then add more specific behaviors after that. So do this:

val foo = mock[Foo]
foo.bar(anyString) throws mock[FooException]
foo.bar("x") returns 1
foo.bar("y") returns 2

Related Query

More Query from same tag