ScalaのTestクラスで以下のようなものを書くと
import org.specs2.mock.Mockito
import play.api.test.PlaySpecification
case class A1(id: B)
case class A2[T](id: T)
case class B(id: Int)
class HogeSpec extends PlaySpecification with Mockito {
override def is =s2"""
これはパスする $tes1
これはパスする $tes2
これはパスする $tes1s
これはパスしない $tes2s
"""
def tes1 = {
val a = mock[A1]
val b = mock[B]
a.id returns b
a.id must_== b
}
def tes2 = {
val a = mock[A2[B]]
val b = mock[B]
a.id returns b
a.id must_== b
}
def tes1s = {
val a = smartMock[A1]
val b = smartMock[B]
a.id returns b
a.id must_== b
}
def tes2s = {
val a = smartMock[A2[B]]
val b = smartMock[B]
a.id returns b
a.id must_== b
}
}
tes2s
の3行目でMockitoが ClassCastException
を返し、a.id
と b
の型が異なるというエラーを吐く
http://stackoverflow.com/questions/10324063/mockito-classcastexception
http://mockito.googlecode.com/svn/tags/1.8.3/javadoc/org/mockito/Mockito.html#RETURNS_DEEP_STUBS
Good quote I've seen one day on the web: every time a mock returns a mock a fairy dies.
エラーで検索したら、「モックがモックを返すたび妖精が死ぬ」という謎の格言が出てきた
追記
どうやらsmartMock
でなく mock
を用いた場合はエラーにならない。