Thursday, February 13, 2014

Testing your RESTful services with Groovy, Spock and HTTPBuilder

I like to test the HTTP portion of the RESTful web services I build in isolation. This is because, in the languages I use (Java, Scala), most of the code responsible for taking in HTTP requests and parsing them is very framework specific and simply delegates the actual logic to another part of my application. Ideally I can launch this part of my application in the same way as it is launched in production but with the rest of the application stubbed out.

Recently I've been using Groovy/Spock for these integration tests, here's how for a really simple web service. In this example it's been built with Spring Boot.

Let's not go into the details of how Spring boot work but it is essentially the same application as in Spring's guide.

The web application has a single path: /exampleendpoint which takes in a single query param: input and returns a JSON object with a single field payload with the value: Something really important: with the input appended.

I want to test that the web service takes in HTTP, takes the query parameter out and builds the JSON correctly. With Groovy/Spock/HTTPBuilder here's how it looks:

Let's look at what is going on here:
  • In the setup phase we create a RESTClient
  • In the exercising phase we make a call out to the web application.
  • In the verification phase we check that the response code is 200, the content type is "application/json" and the body is JSON with a single payload field that contains "Something really important: Get a hair cut"
Benefits over writing this in Groovy/Spock over say just using Java/JUnit:
  • The HTTP libraries are easier to use with less code
  • The assertions are more expressive
  • More magic, for instance the second with() block works on a Groovy map that has automagically created
The above example doesn't deal with how to start and stop the web service under test, I left that out as it is very web framework specific. 

What do you need in your build to get this setup? I used Gradle for this example so it is probably easier to just check out the build.gradle, a lot of it can be ignored as it is related to Spring Boot rather than Spock. Here are the important bits:

The plugins:

And the dependencies:

The entire project is here. Happy testing.

24 comments:

  1. This is really helpful. Thanks

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I agree that this is helpful. Unfortunately the project 404s in Github. Did that project contain additional examples? I'd be interested in seeing more. Thanks!

    ReplyDelete
  4. Testing makes your customer to get full satisfaction on your service since it found out all the bugs and errors and rectify it. Selenium is the best tool ever to test web-based applications. Thank you for your information.

    Regards:
    Selenium Training in Chennai | Selenium Training

    ReplyDelete
  5. It is very useful information. It will help to improve my knowledge in Selenium. Thank you for sharing this awesome site.
    Selenium training Chennai | Selenium testing training in Chennai | Best selenium training in Chennai | Selenium classes in Chennai

    ReplyDelete
  6. Nice Blog, Thanks for sharing this valuable one. This very useful for me and gain more information. Regards,
    Selenium Training in Chennai

    ReplyDelete
  7. I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Artificial Intelligence Training in Bangalore. Keep sharing your information regularly for my future reference.

    ReplyDelete
  8. Thanks for sharing informative post. Looking for best cleaning companies Sunshine Coast? We are the professional cleaners offering bond cleaning & other cleaning service at affordable price.

    ReplyDelete
  9. I read this blog,Thanks for sharing this information.

    Full Stack classes in Mumbai

    ReplyDelete
  10. Really very nice article. Interesting and knowledgeable stuff. I enjoyed while reading this article. Thanks for sharing with us.
    Data Science Training in Hyderabad

    ReplyDelete
  11. Thank you for sharing such a really admire your post. Your post is great!. Pet Dentistry in Vizag

    ReplyDelete
  12. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.I want to share about . . Sweet Shops in Hyderabad

    ReplyDelete
  13. You can install it on your computer and your mobile phone (Android, iPhone, Windows Phone, Etc.) In addition, you will be able to download the Savefrom extension for your web browser. It’s been referred to as savefrom.net helper. It allows you to download any video online directly from official websites. en.savefrom.net remove

    ReplyDelete
  14. Thanks for sharing wonderful post. If you are a students and you don't know "how to start a personal essay writing" then you can visit this excellent guide: how to start a personal essay writing

    ReplyDelete