Recently I've been writing an application which both produces and consumes data via REST. It's been a while since I've done anything like this, and of course the tools and technology have moved on. I wanted to share a couple of useful tools that I've come across along the way. Both of these have come in very handy in developing and debugging my application.
This is a Java DSL for including REST services in unit tests. Why I wanted this requires a little background explanation. I'm a proponent of creating unit tests for software I'm developing as I go along, and for Java development I've always used JUnit. However with a REST service, it's a little trickier - of course there are individual Java classes that one can write JUnit tests for, but when it comes to testing the whole application, it's more difficult. For example, I know that my application should return certain data over HTTP when it receives a GET or POST request, or that it should return a certain error message when it receives bad or missing data, but how to test that?
That's where REST-assured comes in. It's a Java DSL (Domain Specific Language) which allows you to use certain non-standard syntax in your code. For example, let's say my REST service can accept POST requests with certain specific and is supposed to return some XML, and if all is well the HTTP status code should be 200. To test this using REST-assured you can write (I've split this over multiple lines for clarity):
given().param("x", "some value of parameter x")
Obviously the real parameter names and values are different, but I hope you get the idea. REST-assured has a lot of other facilities for testing just about anything you want, and makes it easy to do both development-time and regression testing of REST services.
One thing that I stumbled over initially though, probably due to my unfamiliarity with DSLs was how to get the Java compiler (in my case the one inside Eclipse) to recognise the DSL syntax. There are two steps to this:
- Telling the compiler about the location of the REST-assured .jar file. In Eclipse this is done via Project > Properties > Java Build Path and adding the .jar as a library (you can create a User Library via the Eclipse Preferences if you're going to be doing this in multiple projects). For command-line applications, the jar file needs to be in the classpath, specified via the
- Importing the relevant classes. This uses Java's static import syntax:
import static com.jayway.restassured.RestAssured.*;
Once you've done both of these, you should be able to compile and run your tests.
This is an extension for the Google Chrome browser for analysis of REST applications. It allows you to specify settings and parameters for HTTP requests to your application, and analyse the responses. It's very useful for making the client/server communication process more transparent. Chrome is my browser of choice, however I'm sure there are equivalent tools for Firefox and other browsers.
I hope you find these tools useful - they've certainly helped me.