BZapper
BZapper

Reputation: 330

Mockito Scala Specs2 mocking complex functions (Play Framework)

I'm trying to properly stub the ehCache API used by Play Framework. In particular, its getOrElse function with signature:

def getOrElse[A: ClassTag](key: String, expiration: Duration)(orElse: => A) 

Within my specs 2 code, I have:

val mockCache = mock[EhCacheApi]
mockCache.getOrElse[???](anyString,anyObject[Duration])(???) returns 
[Object I'd like returned]

Question is if it's possible to use matchers for the ??? parts, especially for the currying argument part.

The return type for the CacheApi function should be Future[Seq[Object]] .

Public git repo link: Github

Upvotes: 1

Views: 769

Answers (1)

Eric
Eric

Reputation: 15557

This works

class VariationAssignmentSpec(implicit ee: ExecutionEnv) extends PlaySpecification with Mockito {

  case class Variation(id: Option[Long] = None)

  lazy val v1 = Variation(Option(1L))
  lazy val v2 = Variation(Option(2L))

  "Cache#getOrElse" should {
    "return correct result" in {

      val mockCache = mock[CacheApi]

      mockCache.getOrElse[Future[Seq[Variation]]](anyString, any[Duration])(any)(any) returns
        Future(Seq(v1, v2))

      val resultFuture: Future[Seq[Variation]] =
        mockCache.getOrElse("cache.key", 10.seconds)(Future(Seq(v1,v2)))

      resultFuture must equalTo(Seq(v1,v2)).await
    }
  }

}

Upvotes: 2

Related Questions