All Projects → stephennancekivell → scalatest-json

stephennancekivell / scalatest-json

Licence: Apache-2.0 license
Scalatest matchers for Json with appropriate equality and descriptive error messages.

Programming Languages

scala
5932 projects
shell
77523 projects

Projects that are alternatives of or similar to scalatest-json

scala-basic-skeleton
Starting point if you want to bootstrap a project in Scala
Stars: ✭ 16 (-51.52%)
Mutual labels:  scalatest
docker-controller-scala
No description or website provided.
Stars: ✭ 12 (-63.64%)
Mutual labels:  scalatest
scalatest-junit-runner
JUnit 5 runner for Scalatest
Stars: ✭ 30 (-9.09%)
Mutual labels:  scalatest
cats-effect-testing
Integration between cats-effect and test frameworks
Stars: ✭ 155 (+369.7%)
Mutual labels:  scalatest

Scalatest-json

Build Status Maven Central

Scalatest matchers for Json with appropriate equality and descriptive error messages.

Install

Add the dependency you want

libraryDependencies += "com.stephenn" %% "scalatest-json-jsonassert" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-json4s" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-play-json" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-circe" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-argonaut" % "0.2.0"
libraryDependencies += "com.stephenn" %% "scalatest-jsoniter-scala" % "0.2.0"

You can also check the latest version on maven central.

Usage

Your scalatest spec should extend or import JsonMatchers

class ExampleSpec extends FunSpec with Matchers with JsonMatchers {

  describe("JsonMatchers usage") {
    it("should pass matching json with different spacing and order") {
      val input = """
        |{
        | "some": "valid json",
        | "plus": ["json", "content"]
        |}
      """.stripMargin

      val expected = """
                    |{
                    | "plus": ["json", "content"],
                    |     "some":   "valid json"
                    |}
                  """.stripMargin

      input should matchJson(expected)
    }

    it("should fail on slightly different json explaining why") {
      val input = """
                    |{
                    | "someField": "valid json"
                    |}
                  """.stripMargin

      val expected = """
                       |{
                       | "someField": "different json"
                       |}
                     """.stripMargin

      input should matchJson(expected)
//
//      Fails
//
//      Json did not match {
//      "someField": "valid json"
//      } did not match {
//      "someField": "different json"
//      }
//
//      Json Diff:
//        someField
//      Expected: different json
//        got: valid json

    }
  }
}

For Jsoniter Scala the data structures and corresponding codec shall be defined, eg.:

  import com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec
  import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker

  case class Data(some: String, plus: Seq[String])
  
  implicit val codec: JsonValueCodec[Data] = JsonCodecMaker.make

Please find a complete example here.

Golden Test

For the circe module you write a golden test that encodes and decodes a model, checking that it matches a golden example json.

it("can compare json and a model") {
    case class Foo(sameField: String)
    implicit val encoder = Encoder.forProduct1("someField")(Foo.unapply)
    implicit val decoder = Decoder.forProduct1("someField")(Foo.apply)
    
    val json = """
                |{
                | "someField": "valid json"
                |}
              """.stripMargin
    
    val model = Foo("valid json")
    
    model should matchJsonGolden(json)
}

Publishing

Done from github actions using sbt-ci-release

eg

./release 'v0.2.5'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].