Using an eq matcher with java.lang.UUID in Specs2 Mockito

1.2k Views Asked by At

We recently changed the API on one of our services, it used to be:

def updateSubtitle(subtitleId: String...): Subtitle

Now it is:

def updateSubtitle(subtitleId: UUID, ...): Subtitle

And previously we wrote our expectations like so:

there was one(subtitleService).updateSubtitle(eq(subtitleId), ...)

This won't work anymore because subtitleId is now a UUID instead of a String. I've had to change eq(subtitleId) to any[UUID] however this is too generic as it doesn't actually test for the subtitleId value, it only cares that a value of type UUID was passed.

How can I get an eq matcher to work with UUID?

1

There are 1 best solutions below

1
On BEST ANSWER

eq(subtitleId) does work with UUID because the UUID.equals method is correctly implemented (https://docs.oracle.com/javase/6/docs/api/java/util/UUID.html#equals(java.lang.Object).

You may be having issues with the naming clash between scala.AnyRef.eq and org.mockito.Matchers.eq (see https://github.com/etorreborre/specs2/issues/361). This can be solved by either:

  • Fully referencing the Matchers.eq (i.e. one(subtitleService).updateSubtitle(org.mockito.Matchers.eq(subtitleId), ...)) or
  • Using Scala import aliases to change the Matchers.eq name (i.e. import org.mockito.Matchers.{eq => meq, _} and changing your matcher use accordingly to meq(subtitleId)