Skip to content

Instantly share code, notes, and snippets.

@kasia-kittel
Last active November 22, 2018 11:36
Show Gist options
  • Save kasia-kittel/3255fd5dc5f8f861f4e02bb966302c1c to your computer and use it in GitHub Desktop.
Save kasia-kittel/3255fd5dc5f8f861f4e02bb966302c1c to your computer and use it in GitHub Desktop.
Avoid NullPointerException when stubbing AnyVal using Mockito
you need to stub some opeation like:
when(formDAO.addFile(any[InputStream], eq(special.id))) thenReturn Right(FileId(1))
and:
case class SpecialId(id: Long) extends AnyVal {
override def toString: String = id.toString
}
since "AnyVal is the root class of all value types, which describe values not implemented as objects in the underlying host system."
and that confuses Mockito matchers...
the workaroud is:
val anySpecialId: SpecialId = SpecialId(org.mockito.Matchers.any[Long])
when(formDAO.addFormFile(any[FileInputStream], anySpecialId )) thenReturn Right(FileId(1))
now stubbed methond works corectly
(found at http://stackoverflow.com/questions/27289757/mockito-matchers-scala-value-class-and-nullpointerexception)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment