0

I have a huge json object and I need to parse it and then write some tests to see if everything goes as expected.

case class User(id: Identification, age:Int, name: String ...)
case class Identification(id: Int, hash: String ....)
... a lot more classes

Now I'm trying to write the tests

val json = parse(Source.fromFile(/path).getLines.mkString("\n"))
import org.json4s.DefaultFormats
implicit val formats = DefaultFormats

So my question is how can i test if the case classes are ok? I thought maybe I should try to extract for ex. the users and then to check parameter by parameter if they are correct, but I don't thing that is a good way because it is not me who created the json so I'm not interested about the content.

Thanks

lads
  • 1,125
  • 3
  • 15
  • 29

1 Answers1

2

This is what I found working with JSON and case classes overt the years the minimum to test.

This three things should be tested always

Serialization with deserialiaztion combined

val example = MyCaseClass()
read[MyCaseClass](write(example)) should Equal example

Checks if a class can be transformed to JSON, read back and still has the same values. This one breaks more often than one would think.

Deserialization: JSON String -> CaseClasses

val exampleAsJSON : String
val exampleAsCaseClass : MyCaseClass

read(exampleAsJSON) shouldEqual exampleAsCaseClass

Checks if JSON still can be deserialized.

Serialization: CaseClasses -> JSON String

 val exampleAsJSON : String
 val exampleAsCaseClass : MyCaseClass

 write(exampleAsCaseClass) shouldEqual exampleAsJSON

Checks if String/ JSON Representation stays stable. Here it is hard to keep the data up to date and often some not nice whitespace changes lead to false alarms.

Additional things to test

Are there optional parameters present? If yes all tests should be done with and without the optional parameters.

Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52
  • Normaly: If the object can be serialized it can be also use when it is contained within another object. So you only need a custom serializer for the special cases. – Andreas Neumann Mar 07 '16 at 09:34