Skip to content

Instantly share code, notes, and snippets.

@hgiddens
Created November 27, 2012 02:44
Show Gist options
  • Select an option

  • Save hgiddens/4152064 to your computer and use it in GitHub Desktop.

Select an option

Save hgiddens/4152064 to your computer and use it in GitHub Desktop.
Why does this work?
"wtf" in {
trait Consumer { def consume(value: Root): Unit }
sealed trait Root
case class Alpha(s: String) extends Root
case class Beta(s: String) extends Root
val consumer = mock[Consumer]
consumer.consume(Alpha("ss"))
consumer.consume(Beta("ss"))
there was two(consumer).consume(any[Alpha]) // I would expect this to fail but it doesn't.
}
@etorreborre
Copy link

This seems to be a Mockito issue. If I replace the specs2 syntactic sugar by some pure Mockito calls the example also passes ok:

import org.specs2.mutable._
import org.specs2.mock._
import org.mockito.{Mockito => M, Matchers => MM}
import M._

class Test extends Specification with Mockito { nocolor
  "wtf" in {
    trait Consumer { def consume(value: Root): Unit }
    sealed trait Root
    case class Alpha(s: String) extends Root
    case class Beta(s: String) extends Root

    val consumer = mock[Consumer]
    consumer.consume(Alpha("ss"))
    consumer.consume(Beta("ss"))
    M.verify(consumer, M.times(2)).consume(MM.any(classOf[Alpha])) // I would expect this to fail but it doesn't.
  }
}

specs2.run(new Test)

scala> specs2.run(new Test)
specification

+ wtf

Total for specification specification
Finished in 8 ms
1 example, 0 failure, 0 error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment