title: How to test a play framework controller that uses a custom bodyparser?
body: <p>I'm trying to test a play framework 2.5.10 controller that uses a custom bodyparser and JSON validator:</p>
  
  <pre><code>class MessagingController {
    def validateJson[A : Reads] = parse.json.validate(
      _.validate[A].asEither.left.map(e =&gt; BadRequest(JsError.toJson(e)))
    )
    def createMessageThread() = Action(validateJson[InboundMessageThread]) { request =&gt; 
      Ok("OK") }
    }
  </code></pre>
  
  <p>When I run a simple test case on this I get an error:</p>
  
  <pre><code>For request 'POST /api/v1/messageThreads' [Invalid Json: No content to map due to 
  end-of-input at [Source: akka.util.ByteIterator$ByteArrayIterator$$anon$1@5693d1d2; line: 1, column: 0]]
  </code></pre>
  
  <p>The test is:</p>
  
  <pre><code>  val fakeRequest = FakeRequest()
        .withHeaders(HeaderNames.CONTENT_TYPE -&gt; "application/json")
        .withBody(Json.parse(
          s"""
             |{
             |  "participants": [
             |   {"id": $currentUserId, "isAdmin": false}
             |  ],
             |  "isGroupThread": false
             |}
      """.stripMargin))
  
    val result =  messagingController.createMessageThread()(fakeRequest).run()
    status(result) mustBe OK
  </code></pre>
  
  <p>If I change the controller's action to just <code>Action</code> and then validate the JSON in the body of the controller it works. I don't want to do that though because lots of my REST controllers need to parse JSON and using <code>validateJson[T]</code> reduces boilerplate. The code also works if I submit the same payload using curl.</p>
  
  <p>How can I test this controller?</p>