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")
}
}