I have some restful services using Scala and play framework (2.4.*) and I am trying to write some functional tests for those services and getting hard time on negative assertions. For example :
If I receive a response ( in Json) from my service like :
{ "id":71324, "name":"Matt", "address":"24 Main st" }
I am trying to check that :
Finding hard time to find an example on how to do the above to assertion.
For other assertions, I did it this way:
class IntegrationTest extends PlaySpec with OneServerPerSuite with MockitoSugar { // need with mockito*?
override lazy val app = new GuiceApplicationBuilder()
.configure(Configuration.apply(ConfigFactory.parseFile(new File("test/resources/testApplication.conf")).resolve()))
.overrides(bind[EmployeeDAO].to[MockEmployeeDAO])
.build
implicit lazy val log = LoggerFactory.getLogger(getClass)
val wsClient = app.injector.instanceOf[WSClient]
val myPublicAddress = s"localhost:$port"
"test1" must {
"get employee record" in {
val route = s"http://$myPublicAddress/INTERNAL/employee/7"
val response = await(wsClient.url(route).get())
log.debug(response.header("Content-Type") + " -> " + response.body)
val jsonResponse = Json.parse(response.body)
response.status mustBe OK
(jsonResponse \ "id").get mustBe JsNumber(71324)
(jsonResponse \ "name").get mustBe JsString("Matt")
//trying to check that there is no phone
//trying to check address fiels exists and is non-empty
//(jsonResponse \ "address").get mustNot empty -- got systax error
}
}
}
May I get help here?
There are few problems with
(jsonResponse \ "address").get mustNot empty
The type of (jsonResponse \ "address")
is JsLookupResult
which has isEmpty()
method, hence by checking for emptiness it seems reasonable to try
(jsonResponse \ "address") mustNot be (empty)
However this does not work because empty
keyword DSL works on following types
scala.collection.GenTraversable
String
Array
scala.Option
java.util.Collection
java.util.Map
isEmpty()
method that returns Boolean
parameterless
isEmpty method that returns Boolean
where by "arbitary object" they really mean arbitrary reference object AnyRef
implicit def emptinessOfAnyRefWithIsEmptyMethod[T <: AnyRef { def isEmpty(): Boolean}]: Emptiness[T]
and JsLookupResult
is not a subtype of AnyRef
sealed trait JsLookupResult extends Any with JsReadable
Therefore, because constraint T <: AnyRef { def isEmpty(): Boolean}
is not satisfied, we cannot use the nice emptiness DSL.
Alternatively the following should work
(jsonResponse \ "address").isEmpty mustBe false
(jsonResponse \ "address").isDefined mustBe true