我正在编写一个模拟对象,如下所示:
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...试过了
这个案子的正确论据是什么?非常感谢。
发布于 2013-09-26 23:30:42
您需要使用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)https://stackoverflow.com/questions/19032163
复制相似问题