将 Scala 的值类与 Mockito Mocks (Matchers) 一起使用会导致 NullPointerException,请参见以下示例:
import org.scalatest.FunSuite
import org.scalatest.mock.MockitoSugar._
import org.mockito.{Matchers, Mockito}
class Id(val underlying: Int) extends AnyVal
class MockitoValueClassSuite extends FunSuite {
trait Sample {
def doIt(id: Id) : Int
}
test("Check value classes in scala with mockito") {
val sampleMock = mock[Sample]
val id = new Id(1)
Mockito.when(sampleMock.doIt(Matchers.eq(id))).thenReturn(5)
assert(sampleMock.doIt(id) == 5)
}
}
结果是:
java.lang.NullPointerException was thrown.
java.lang.NullPointerException
at net.gutefrage.service.user.service.MockitoValueClassSuite$$anonfun$1.apply$mcV$sp(MockitoValueClassSuite.scala:21)
at net.gutefrage.service.user.service.MockitoValueClassSuite$$anonfun$1.apply(MockitoValueClassSuite.scala:16)
at net.gutefrage.service.user.service.MockitoValueClassSuite$$anonfun$1.apply(MockitoValueClassSuite.scala:16)
at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
任何提示可以在这里做什么?