LoginSignup
4
4

More than 5 years have passed since last update.

smartMockを入れ子にしたときジェネリック型が使えない

Last updated at Posted at 2015-08-12

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.idb の型が異なるというエラーを吐く

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 を用いた場合はエラーにならない。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4