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
}