1

私は以下のようなモックオブジェクトを書いています:

import org.specs2.mock._
import com...MonetaryValue
import com...Voucher
import org.mockito.internal.matchers._

/**
 * The fake voucher used as a mock object to test other components
 */
case class VoucherMock() extends Mockito {
  val voucher: Voucher = mock[Voucher]

  //stubbing
  voucher.aMethod(any(classOf[MonetaryValue])) answers {arg => //some value to be return based on arg} 

  def verify() = {
    //verify something here
  }
}

スタブ ステップで例外がスローされます。

 ...type mismatch;
[error]  found   : Class[com...MonetaryValue](classOf[com...MonetaryValue])
[error]  required: scala.reflect.ClassTag[?]
[error]   voucher.aMethod(any(classOf[MonetaryValue])) answers {arg => //some value to be return based on arg} 

次のように、引数から値を取得し、この引数に基づいて値を返したい: http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#11

私は試してみましたisA, anyObject...

この場合の正しい引数マッチャーは何ですか? どうもありがとうございました。

4

1 に答える 1

6

を使用する必要がありますany[MonetaryValue]。完全に機能する例を次に示します。

class TestSpec extends Specification with Mockito { def is = s2"""
  test ${
    val voucher: Voucher = mock[Voucher]

    // the asInstanceOf cast is ugly and 
    // I need to find ways to remove that
    voucher.aMethod(any[MonetaryValue]) answers { m => m.asInstanceOf[MonetaryValue].value + 1}
    voucher.aMethod(MonetaryValue(2)) === 3
  }
  """
}

trait Voucher {
  def aMethod(m: MonetaryValue) = m.value
}
case class MonetaryValue(value: Int = 1)
于 2013-09-26T23:30:42.607 に答える