Integration testing a REST client

Today I’ve spent some time looking at an interesting problem. I’ve been using the JAX-RS reference implementation Jersey to consume a RESTful web service, it’s really simple. I haven’t played with RESTful web services that much yet but I have to say I really like, the whole idea about using HTTP is brilliant. Anyway, as I was working on this adapter that would consume this RESTful web service I came across a problem, I needed to have an integration test running so that I check that my app was wired up correctly, read in the correct config properties and that the actual Jersey client did what it was supposed to.

I spent some time searching around for some ideas on how to do this and finally came across a really nice article.

Have a look at http://olafsblog.sysbsb.de/lightweight-testing-of-webservice-http-clients-with-junit-and-jetty/. It’s brilliant, basically what you do is you start up Jetty internally in your test class and you have your app point at this running Jetty server. You can then in your test case tell the server what to return on the next request, kind of like a mock response in a way.

Thinking about it now, I might actually see if I can create a project for it on Github and potentially add some more good stuff to it. Also to note is that this mock type server would be able to serve as any HTTP endpoint you wanted to stub out, you simply just tell it what to return and it’s brilliant from an integration point because you’re actually firing off a real HTTP request locally.

Give it a go I tell you, it’s saved my a lot of time and effort.