I'd like to use Spec2 and Scala to test a REST service that was build using Java. I looked at Spray but it seams like you have to build your application using Spray in order to test it using SprayTest. I also found this thread but it's not really what I'm looking for.
Any other ideas?
So, from our perspective the best automation framework for REST APIs is RestAssured. It is simple to use, modular and you can manipulate the request in any way you want.
If you are not a big fan of command-line tools and rather like a GUI client to test your REST API then Postman is the best tool for you. It comes as a Chrome extension and you can install it on your chrome browser and from thereon. It is probably the most popular tool to test your REST API.
We have successfully been testing all of our REST APIs using Specs2 and the Dispatch library (https://dispatchhttp.org/Dispatch.html). Dispatch takes a little bit of time to get your head around, but once you understand how it composes everything together with various operators you can test a simple REST service with a couple of lines of code.
Here's a couple of test cases from out recent project:
def issueErrorStatus = {
val requestBody = "msisdn=447777666666&message=Some test message"
val req = url("http://localhost:%d/otac/issue".format(port)) <<
(requestBody, "application/x-www-form-urlencoded")
val response = Http.when(_ == 400)(req <:< (touchPointHeaders) as_str)
response must_== """{"error":"Message must contain an {OTAC} place holder"}"""
}
def checkOtac = {
val req = url("http://localhost:%d/otac/check".format(port)) <<?
Vector(("msisdn" -> "447777123456"))
val response = Http(req <:< (touchPointHeaders) as_str)
response must_== """{"status":"Present","reissueAllowed":true}"""
}
The first test makes a post request, the second a get request. We also have some more complex tests that parse the response JSON string through the lift-json parser so that we can assert agains the document more easily. The above tests are just checking some simple error/status cases.
There's also a dispatch-reboot project underway that has a simplified API and works with async connections. Not sure how stable it is yet though.
In my last projects I used AsyncHttpClient and Jersey Client for testing REST services and I can recommend both of them. For async operations the former is better (I don't know if jersey client supports async operations at all).
They are written in Java and have (to my knowledge) no Scala-API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With