I am using specs2 and want to test my json reads that I created.
I have my case classes and implicits created like:
object ComputerImplicits {
implicit val partReads = Json.reads[Part]
implicit val computerReads = Json.reads[Computer]
}
I have a sample json file in my test/resources/computer.json
folder.
I am loading the JSON file as a string like this:
val jsonString = Source.fromURL(getClass.getResource("/computer.json")).mkString
I brought the implicits in scope:
import ComputerImplicits._
Now how do I take my case classes and use the json string and attempt to parse it and match it to test if it is working correctly?
I am using Plays json macros https://www.playframework.com/documentation/2.8.x/ScalaJsonAutomated
Assuming you use Play JSON:
final class FooSpec extends org.specs2.mutable.Specification {
"Json" should {
"be ok" in {
Json.parse(jsonString).validate[YourType] must_=== JsSuccess(expectedVal)
}
}
}
Also implicit related to a type are usually declared in its companion object (rather than in a shared object).