top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to test serialisation in Scala?

+1 vote
331 views
How to test serialisation in Scala?
posted Nov 15, 2016 by Dominic

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

In order to check that everything works fine, we can create unit tests. Add the ScalaTest dependency in the SBT file. It should look like this now:

name := "proectj-name"

version := "1.0"

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  "org.scalatest" % "scalatest_2.11" % "3.0.0-SNAP5" % "test",
  "com.typesafe.play" % "play-json_2.11" % "2.4.2")

And write tests for the Payment:

import models._
import models.Payment._

import org.scalatest._
import play.api.libs.json._

class PaymentTest extends FlatSpec with Matchers {

  val address = Address("1375 Burlingame Ave.", None, "Burlingame", "California", "94010")

  "Payment " should "be converted to JSON correctly " in {

    val payment = Payment(1, "creditCard", address, "wdweadowei3209423", "123")
    val paymentJSON = writePayment(payment)

    (paymentJSON \ ("id")).get should be (JsNumber(1))
    (paymentJSON \ ("type")).get should be (JsString("creditCard"))
    (paymentJSON \ ("address")).get should be (Json.toJson(payment.address))
    (paymentJSON \ ("token")).get should be (JsString("wdweadowei3209423"))
    (paymentJSON \ ("cvv")).get should be (JsString("123"))

  }

  it should " be deserialized correctly " in {
    val paymentJSON: JsValue = JsObject(Seq(
      "id" -> JsNumber(1),
      "type" -> JsString("creditCard"),
      "address" -> Json.toJson(address),
      "token" -> JsString("wdweadowei3209423"),
      "cvv" -> JsString("123")
    ))

    val payment = readPayment(paymentJSON)

    payment.id should be (1)
    payment.pType should be ("creditCard")
    payment.address should be (address)
    payment.token should be ("wdweadowei3209423")
    payment.cvv should be ("123")
  }

}
answer Nov 16, 2016 by Karthick.c
Similar Questions
+2 votes

How to define Default parameters in Scala? What is the use of Default Parameters in Scala? How to avoid implementing Auxiliary Constructors or Multiple Constructors in Scala?

+4 votes

Like Java’s Package level Access Modifier, is there anything same concept in Scala? How to provide Package level Access Modifier in Scala?

...