0

I am a bit puzzled why my test fails when introduce json4s parsing in it. The application itself works fine with it.

The application code:

import org.json4s.native.JsonMethods

class PonyService {

  protected def client = new PonyClient // Returns Future[String]

  def getPony(): Future[Pony] = {
    val contentFuture = client.retrieveContent()
    contentFuture.map{case s => extractPony(s)}
  }

  def getPonyJSON(): Future[String] = {
    val contentFuture = client.retrieveContent()
    contentFuture.map{case s => s}
  }

  def extractPony(json: String): Pony = {
    implicit val formats = DefaultFormats
    (parse(json) \ "result").extract[Pony]
  }
}

The test code:

val mockClient = mock[PonyClient]
mockClient.retrieveContent() returns Future.successful {"pony json here"}

val service = new PonyService {
  override protected def client = mockClient
}

whenReady(service.getPony()) {
  r => r must equalTo(SpecifiedPony) // Fail - A timeout occurred waiting for a future to complete.
}

whenReady(service.getPonyJSON()) {
  r => r must equalTo("pony json here") // Success
}
Petteri H
  • 11,779
  • 12
  • 64
  • 94

1 Answers1

1

It turned out to be too short wait in whenReady. For example this works fine:

val pony = service.getPony
val res = Await.result(pony, Duration("1 seconds"))
res must equalTo(SpecifiedPony)
Petteri H
  • 11,779
  • 12
  • 64
  • 94