Now, let’s continue with testing our JSON codec for Product. We need our JSON codec to provide several guarantees:
It must fail to decode invalid JSON input format (garbage values).
It must fail to decode valid JSON input format with invalid data (wrong semantics).
It must successfully be able to decode completely valid input.
It must encode JSON, which contains all fields included in the model.
It must be able to decode JSON that it encoded itself.
The first one is pretty simple, and we don’t have to write a test for this because it should be guaranteed by the Circe library. When encoding to numbers or strings, things begin to look slightly different for straightforward JSON representations. Point 5 ensures that our work is made easier by implementing reversible encoders and decoders.
Testing decoding garbage input
xxxxxxxxxx
forAll("input") { s: String =>
decode[Product](s).isLeft must be(true)
}