问题描述
使用类型化值类作为 ID 是 Scala 中的一种常见模式.但是,在对将值类作为参数的方法进行存根时,Mockito 似乎存在问题.在下面的示例中,带有实际值的第一个存根工作正常,但使用参数匹配器的第二个存根抛出 NullPointerException.
我发现的唯一参考是这个问题 但那里显示的解决方案不起作用.任何人都知道解决方案或解决方法?
版本为:org.mockito:mockito-all:1.10.19 和 org.specs2:specs2_2.11:2.4.15
import org.specs2.mutable.Specification导入 org.specs2.matcher.Matchers导入 org.specs2.mock.Mockitocase 类 ID[T](val id:Long) 扩展 AnyVal特质 DAO[T]{def get(id:ID[T]):T}类 MockitoIDStubTest 使用带有匹配器的 Mockito 扩展规范{Mockito"应该{{中的带有参数值的正确存根"val m = 模拟[DAO[字符串]m.get(ID[String](1)).returns("abc")m.get(ID[String](1)) must_== "abc"}使用参数匹配器正确存根"{val m = 模拟[DAO[字符串]m.get(any[ID[String]]).returns("abc")m.get(ID[String](1)) must_== "abc"}}}
[信息] Mockito 应该
[info] + 带有参数值的正确存根
[信息]!使用参数匹配器正确存根
[错误] NullPointerException:(MockitoIDStubTest.scala:20)
[错误] MockitoIDStubTest$$anonfun$1$$anonfun$apply$5$$anonfun$apply$6.apply(MockitoIDStubTest.scala:20)
它似乎适用于 scalamock 和 scalatest.我仍然想为 Mockito 找到一个解决方案,所以我不必更改几百个测试.
import org.scalatest._导入 org.scalamock.scalatest.MockFactorycase 类 ID[T](val id:Long) 扩展 AnyVal特质 DAO[T]{def get(id:ID[T]):T}ScalaMockIDStubTest 类使用 MockFactory{ 扩展 WordSpec导入 language.postfixOpsScalaMock"应该{{中的带有参数值的正确存根"val m = 存根 [DAO [字符串](m.get _) 当(ID[String](1)) 返回("abc")断言( m.get(ID[String](1)) == "abc")}使用参数匹配器正确存根"{val m = 存根 [DAO [字符串](m.get _) when(*) 返回("abc")断言( m.get(ID[String](1)) == "abc")}}}
Using typed value classes as IDs is a common pattern in Scala. However, it seems Mockito has an issue when stubbing methods that take value classes as arguments. In the example below, the first stub, with an actual value works just fine, but the second one, that uses an argument matcher throws NullPointerException.
The only reference to this I've found is this question but the solution shown there does not work. Anyone knows a solution to this, or a work-around?
Versions are: org.mockito:mockito-all:1.10.19 and org.specs2:specs2_2.11:2.4.15
import org.specs2.mutable.Specification
import org.specs2.matcher.Matchers
import org.specs2.mock.Mockito
case class ID[T](val id:Long) extends AnyVal
trait DAO[T]{
def get(id:ID[T]):T
}
class MockitoIDStubTest extends Specification with Mockito with Matchers{
"Mockito" should{
"properly stub with argument value" in {
val m = mock[DAO[String]
m.get(ID[String](1)).returns("abc")
m.get(ID[String](1)) must_== "abc"
}
"properly stub with argument matcher" in {
val m = mock[DAO[String]
m.get(any[ID[String]]).returns("abc")
m.get(ID[String](1)) must_== "abc"
}
}
}
It seems to work with scalamock and scalatest. I would still like to find a solution for Mockito tho, so I don't have to change a few hundred tests.
import org.scalatest._
import org.scalamock.scalatest.MockFactory
case class ID[T](val id:Long) extends AnyVal
trait DAO[T]{
def get(id:ID[T]):T
}
class ScalaMockIDStubTest extends WordSpec with MockFactory{
import language.postfixOps
"ScalaMock" should{
"properly stub with argument value" in {
val m = stub[DAO[String]
(m.get _) when(ID[String](1)) returns("abc")
assert( m.get(ID[String](1)) == "abc")
}
"properly stub with argument matcher" in {
val m = stub[DAO[String]
(m.get _) when(*) returns("abc")
assert( m.get(ID[String](1)) == "abc")
}
}
}
这篇关于具有值类参数的 Mockito 存根方法因 NullPointerException 而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!