Using Scala's value classes with Mockito Mocks (Matchers) results in a NullPointerException, see this example:
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)
}
}
Results in:
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)
Any hints what can be done here?
Assuming that the NPE happens on the Mockito.when-line I'd guess it is because the value classes are implemented as actually passing around the primitive and then replacing method calls on it with static methods while Mockito wants to call equals on an object, or something like that. If that is the reason then maybe you could implement your own matcher in Scala to work around it.
In your case, you have to use something like this:
Mockito.when(sampleMock.doIt(Id(Matchers.eq(id)))).thenReturn(5)
or
Mockito.doReturn(5).when(sampleMock).doIt(Id(Matchers.eq(id)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With