I am writing a mock object like below:
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
}
}
The stubbing step throws an exception:
...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}
I want to get the value from the argument and return a value based on this argument, like this: http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#11
I have tried with isA, anyObject...
What is the correct argument matcher for this case? Thank you very much.
You need to use any[MonetaryValue]
. Here is a fully working example:
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)