title: Howto test Custom Json Objects with Spray Routing
body: <p>I'm creating a Rest API with spray-routing on top of mongodb for some CRUD operations, this all works fine, expect whenever I try to test it with specs2
  the following specification</p>
  
  <pre><code>class RestServiceSpec extends Specification with Specs2RouteTest with RoutingRestService
  
    // database initialization removed for clarity
  
    "The rest service" should
      "have a player called 'Theo TestPlayer' in the db" in {
        Get("/api/1.0/player/" + player1._id) ~&gt; restRoute ~&gt; check {
          entityAs[Player] must be equalTo(player1)
        }
      }
    }
  
  // some more specs removed for clarity
  }
  </code></pre>
  
  <p>it will fail with the following error:</p>
  
  <pre><code>MalformedContent(invalid ObjectId ["51308c134820cf957c4c51ca"],Some(java.lang.IllegalArgumentException: invalid ObjectId ["51308c134820cf957c4c51ca"])) (Specs2Interface.scala:25)
  </code></pre>
  
  <p>I have no idea where to look as the reference to the source file and line number point to a generic failTest(msg:String) method</p>
  
  <p>some more info:</p>
  
  <p>I have a case class that I persist to Mongo using SalatDAO</p>
  
  <pre><code>case class Player(@Key("_id") _id:ObjectId = new ObjectId(), name:String, email:String, age:Int) {}
  </code></pre>
  
  <p>where ObjectId() a class is that wraps mongodb's ID generation
  to get this (un)marshalled through spray_json I created some jsonFormats</p>
  
  <pre><code>object MyJsonProtocol {
    implicit val objectIdFormat = new JsonFormat[ObjectId] {
      def write(o:ObjectId) = JsString(o.toString)
      def read(value:JsValue) = new ObjectId(value.toString())
    }
    implicit val PlayerFormat = jsonFormat(Player, "_id", "name", "email", "age")
  </code></pre>
  
  <p>and the relevant part of my route (removed error handling and logging):</p>
  
  <pre><code>  path("player" / "\\w+".r) {id:String =&gt;
      get {
        respondWithMediaType(`application/json`) {
          complete {
            PlayerCRUD.getById(id) 
          }
        }
      } ~
  </code></pre>