Mockito matchers, scala value class and NullPointException

1,214 views
Skip to first unread message

Marek Kliś

unread,
Dec 4, 2014, 4:34:31 AM12/4/14
to moc...@googlegroups.com
Hi all,

I'm using mockito with scalatest. I have following problem when using matchers with value class.

import org.scalatest.FlatSpec
import org.scalatest.mock.MockitoSugar
import org.mockito.BDDMockito._
import org.mockito.Matchers.any

case class FirstId(val value: String) extends AnyVal
case class SecondId(val value: String) extends AnyVal

trait MockedClass {
  def someMethods(firstId: FirstId, secondId: SecondId): Int
}

class ValueClassSpec() extends FlatSpec with MockitoSugar {

  val mockedClass = mock[MockedClass]
  val secondId = SecondId("secondId")

  "Matchers" should "work for value class" in {
    // given
    given(mockedClass.someMethods(any[FirstId], org.mockito.Matchers.eq(secondId))).willReturn(3)
    // when
    val result = mockedClass.someMethods(FirstId("firstId"), secondId)
    // then
    assert(result == 3)
  }
}


and the result is:

ValueClassSpec:
Matchers
- should work for value class *** FAILED ***
  java.lang.NullPointerException:
  at io.scalac.fow.party.ValueClassSpec$$anonfun$1.apply$mcV$sp(ValueClassSpec.scala:22)
  at io.scalac.fow.party.ValueClassSpec$$anonfun$1.apply(ValueClassSpec.scala:20)
  at io.scalac.fow.party.ValueClassSpec$$anonfun$1.apply(ValueClassSpec.scala:20)
  at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
  at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
  at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
  at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
  at org.scalatest.Transformer.apply(Transformer.scala:22)
  at org.scalatest.Transformer.apply(Transformer.scala:20)
  at org.scalatest.FlatSpecLike$$anon$1.apply(FlatSpecLike.scala:1639)
  ...


I found similar question on stackoverflow (http://stackoverflow.com/questions/19974493/scala-value-classes-and-mockito-matchers-dont-play-together) but without any advice.

Is there any posibility to use mockito matchers with scala value class?

Lib versions: scala 2.11.2, mockito 1.10.8, scalatest 2.1.6

I posted it to stackoverflow too - http://stackoverflow.com/questions/27289757/mockito-matchers-scala-value-class-and-nullpointexception

Best regards,
Marek Kliś

Szczepan Faber

unread,
Dec 4, 2014, 4:55:21 AM12/4/14
to moc...@googlegroups.com
Hey,

I'm not sure, I'm not using scala too much these days. any() returns
null just like majority of matcher methods. You might work around the
problem by using anyObject() matcher. I'm open to changes (pull
requests ;) that help with scala integration.

Cheers!
> --
> You received this message because you are subscribed to the Google Groups
> "mockito" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mockito+u...@googlegroups.com.
> To post to this group, send email to moc...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mockito.
> For more options, visit https://groups.google.com/d/optout.



--
Szczepan Faber
Core dev@gradle; Founder@mockito

Marek Kliś

unread,
Dec 4, 2014, 5:55:02 AM12/4/14
to moc...@googlegroups.com
Hi,

I found solution:

val anyFirstId: FirstId = any[String].asInstanceOf[FirstId]
val eqSecondId: SecondId = org.mockito.Matchers.eq[String](secondId.value).asInstanceOf[SecondId]
given(mockedClass.someMethods(anyFirstId, eqSecondId)).willReturn(3)

Best regards,
Marek Kliś

Przemek Piotrowski

unread,
Apr 11, 2015, 6:21:59 AM4/11/15
to moc...@googlegroups.com
Has somebody found workaround to the case, when value class wraps an Int or Long? 
Reply all
Reply to author
Forward
0 new messages