play framework 2: test a request with a json string as body

opensas picture opensas · Oct 29, 2012 · Viewed 8.5k times · Source

I have the following action

def save() = Action(parse.json) { implicit request =>
  request.body.asOpt[IdeaType].map { ideatype =>
    ideatype.save.fold(
      errors => JsonBadRequest(errors),
      ideatype => Ok(toJson(ideatype))
    )
  }.getOrElse     (JsonBadRequest("Invalid type of idea entity"))
}

And I'd like to test it

The web service works ok with curl like this:

curl -X post "http://localhost:9000/api/types" 
--data "{\"name\": \"new name\", \"description\": \"new description\"}" 
--header "Content-type: application/json"

which correctly returns the new resource

{"url":"/api/types/9","id":9,"name":"new name","description":"new description"}

I'm trying to test it with

"add a new ideaType, using route POST /api/types" in {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {

    val json = """{"name": "new name", "description": "new description"}"""

    val Some(result) = routeAndCall(
      FakeRequest(
        POST, 
        "/api/types",
        FakeHeaders(Map("Content-Type" -> Seq("application/json"))), 
        json
      )
    )

    status(result) must equalTo(OK)
    contentType(result) must beSome("application/json")
    val Some(ideaType) = parse(contentAsString(result)).asOpt[IdeaType]

    ideaType.name mustEqual "new name"

  }
}

But I'm getting the following error:

[error] ! add a new ideaType, using route POST /api/types
[error]     ClassCastException: java.lang.String cannot be cast to play.api.libs.json.JsValue (IdeaTypes.bak.scala:35)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:36)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:35)
[error] play.api.mvc.Action$$anon$1.apply(Action.scala:170)

I followed the advices on this question: Play 2 - Scala FakeRequest withJsonBody

Am I missing something?

--

Kim Stebel solution worked fine, but then I tried with withJsonBody, like this:

    val jsonString = """{"name": "new name", "description": "new description"}"""
    val json: JsValue = parse(jsonString)

    val Some(result) = routeAndCall(
      FakeRequest(POST, "/api/types").
      withJsonBody(json)
    )

and I get the following error:

[error] ! add a new ideaType, using route POST /api/types
[error]     ClassCastException: play.api.mvc.AnyContentAsJson cannot be cast to play.api.libs.json.JsValue (IdeaTypes.bak.scala:35)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:36)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:35)

any idea?

Answer

Kim Stebel picture Kim Stebel · Oct 29, 2012

You probably need to pass in a JsValue as the body of the request. Change the line

val json = """{"name": "new name", "description": "new description"}"""

to

val jsonString = """{"name": "new name", "description": "new description"}"""
val json = Json.parse(jsonString)