Consider following types structure:
trait HasId[T] {
def id: T
}
case class Entity(id: Long) extends HasId[Long]
Let's say, we want to mock Entity class in some test.
val entityMock = mock[Entity]
Mockito.when(entityMock.id).thenReturn(0)
Playing such test results in throwing NullPointerException (in second line), probably because of scala compiler behaviour with wrapping primitive types (if we replace Long with String, test executes correctly).
An exception or error caused a run to abort.
java.lang.NullPointerException
at com.test.Entity$MockitoMock$1085095743.id(Unknown Source)
at com.test.Test.<init>(Test.scala:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at org.scalatest.tools.Runner$.genSuiteConfig(Runner.scala:1422)
at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$8(Runner.scala:1236)
This error affects only case classes, and mockito in 2.X versions.
Is there any known solution to handle this issue?
UPDATE: problem occurs in versions newer than 2.0.8-beta