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.

54 comments:

Anonymous said...

Thanks for sharing the information.

For more info : Mobile Apps Solutions

DAMROX said...

This is really helpful. Thanks

chbatey said...

No problem!

Aniket Shirawadekar said...
This comment has been removed by the author.
Unknown said...

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!

Unknown said...

Great Work. This post is worth everyone’s attention. web design company in chennai

Unknown said...

Informative article, just what I was looking for.seo services chennai

Winwebsolutionz said...

Can truly relate and retain this outstanding post. Very well written. web design company Chennai

Suba said...

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

Mathi said...

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

Unknown said...

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

rama said...

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.

Sunshine Eco Cleaning said...

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.

Sprint-Driving-School said...

Great information… Domestic oven detailers are premium over cleaners in Melbourne offering affordable oven & BBQ cleaning by trained professionals.

christopher said...

medicare lcd
medicare denial codes
medicare id format
medicare part c
medicare card images
medicare advantage plan
medicare provider phone number
medicare definition
medicare lcd guidelines

Kayal said...

You did very great work by adding some good information here. I received some ideas here and Thank you...

Tableau Training in Chennai
Tableau Course in Chennai
Power BI Training in Chennai
Job Openings in Chennai
Pega Training in Chennai
Linux Training in Chennai
Placement Training in Chennai
Soft Skills Training in Chennai
Appium Training in Chennai
Tableau Training in Velachery
Tableau Training in OMR

gokul said...

Thank you for this informative blog
Top 5 Data science training in chennai
Data science training in chennai
Data science training in velachery
Data science training in OMR
Best Data science training in chennai
Data science training course content
Data science certification in chennai
Data science courses in chennai
Data science training institute in chennai
Data science online course
Data science with python training in chennai
Data science with R training in chennai

balamurugan said...

Thanks for updating this information. Good job.
German Classes in Chennai
best german classes in chennai
french courses in chennai
pearson vue
Blockchain Training in Chennai
Ionic Training in Chennai 
spanish institute in chennai
content writing training in chennai
German Classes in Anna Nagar
German Classes in Tambaram

priya sharma said...

Such A nice post... thanks For Sharing !!Great information for new guy like Happy New year 2020

anita said...

Such A nice post... thanks For Sharing !!Great information for new guy like Hanuman Chalisa Lyrics

monu said...

Such A nice post... thanks For Sharing !!Great information for new guy like antimalware service executable

sasi said...

Great experience for me by reading this blog. Thank you for wonderful article.
Angularjs Training in Chennai
Angularjs Training in Bangalore
angularjs training institute in bangalore
Angular Training in hyderabad
best angularjs training in bangalore
Angular course in hyderabad
Salesforce Training in Bangalore
Hadoop training in bangalore

priya said...

I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective.
php online training in chennai
php programming center in chennai
php class in chennnai
php certification course
php developer training institution chennai
php training in chennnai
php mysql course in chennai
php institute in chennnai
php course in chennnai
php training with placement in chennnai
php developer course

priya said...

Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
appium online training
appium training centres in chennai
best appium training institute in chennnai
apppium course
mobile appium in chennnai
mobile training in chennnai
appium training institute in chennnai

Anonymous said...

Great efforts put to publish these kinds of articles that are very useful to know. I’m thoroughly enjoying your blog. And Good comments create great relations. You’re doing an excellent job. Keep it up.

Magento Development Training Course in Chennai Zuan Education

Selenium Training Course in Chennai Zuan Education

w3webschool said...

Such a wonderful article and I feel that it is best to write more on this topic. Thank you so much because i learn a lot of ideas about it. Keep posting...
Digital Marketing Course In Kolkata
Web Design Course In Kolkata

OLA cleaning Services said...

Thanks for a great article.We provide complete End of Lease cleaning /General cleaning Service that includes Dusting & Moping in Kitchen area, Bathroom area, floor,windows etc. at best prices. Same day service available.Call Now to book an appointment at +61 0414 534 770.

Mithun said...

Nice Blog post..very interesting to read SAP Training in Chennai | AWS Training in Chennai

Tasya Timothy said...

judi bola deposit pulsa tri

agen bola terpercaya sbobet88

situs sbobet88 asia

deposit sbobet88 pakai pulsa

deposit bola pakai pulsa tri

daftar akun sbobet88

agen sbobet88 asia

sbobet deposit pulsa tri

sbobet deposit pulsa telkomsel

sbobet deposit pulsa xl

Tasya Timothy said...

Login Live22

Link Slot Live22 Terbaru

Situs Live22 Indonesia

Login Live22 Slot

Login Live22 Casino

Cara Login Live22

Link Alternatif Live22

Pathway for German Language said...
This comment has been removed by the author.
Data Science Course Training in Hyderabad said...

Thank you for this article. Its really awesome
Data Science Course in Hyderabad
Data Science Training in Hyderabad

Anonymous said...

I read this blog,Thanks for sharing this information.

Full Stack classes in Mumbai

nayar said...

Great post. keep sharing such a worthy information
Software Testing Course in Bangalore
Software Testing Course in Hyderabad
Software Testing Course in Pune
Software Testing Training in Gurgaon

gangaa said...

Great post. keep sharing such a worthy information

Ethical Hacking Course in Chennai
Ethical Hacking course in Bangalore

madhukits said...

SAP BW on Hana training
sap sd training
osb training
oracle scm training
abinitio training
spark training

Unknown said...

Fantastic article I ought to say and thanks to the info. Instruction is absolutely a sticky topic. But remains one of the top issues of the time. I love your article and look forward to more.
Data Science Course in Bangalore

DataScienceEXPO said...

Thank you quite much for discussing this type of helpful informative article. Will certainly stored and reevaluate your Website.
Data Analytics Course in Bangalore

DataBank said...

Nice to be seeing your site once again, it's been weeks for me. This article which ive been waited for so long. I need this guide to complete my mission inside the school, and it's same issue together along with your essay. Thanks, pleasant share.
Data Science training in Bangalore

shreekavi said...

Nice blog! Thanks for sharing this valuable information
IELTS Coaching in Hyderabad
IELTS Coaching in Bangalore
IELTS Coaching in Pune
IELTS Coaching in Gurgaon
IELTS Coaching in Delhi

DataCharge said...

Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .
Data Analytics training in Bangalore

Reshma said...

Amazing Post. keep update more information.
Artificial Intelligence Course in Bangalore
Artificial Intelligence course in Pune
Artificial Intelligence Course in Gurgaon
Artificial Intelligence Course in Hyderabad
Artificial Intelligence Course in Delhi

bruce wayne said...

Great blog.thanks for sharing such a useful information
Salesforce CRM Training in Chennai

Dhilshan said...

Happy to read the informative blog. Thanks for sharing
best selenium training center in chennai
best training institute for selenium in chennai

technologyforall said...

Join our Online Data Science Course program to analyze data, make effective predictions and gain a better understanding of market trends. Create disruptive business models for the topmost industries as we equip you with a sound business analytical & entrepreneurial ideology, alongside an excellent grasp of marketing strategies and trends.

Ramesh Sampangi said...

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

MY PET CLINIC VIZAG said...

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

David Fincher said...

This post is so interactive and informative.keep update more information...
Tally Course in Anna Nagar
Tally course in Chennai

Pavithra Devi said...

Such a good post .thanks for sharing
selenium training in porur
Selenium training in chennai

Pothgaal Sweets said...

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

Aaraa said...

I am inspired with your post writing style & how continuously you describe this topic. . Aaraa couture

milka said...

Great post. keep sharing such a worthy information.
Manual Testing Online Course

Berita Islam said...

sm3ha
x2download
bagishared
bagishared
mxtube
mxtube


bokep indo
bokep indo

bokep hd
bokep indo

nani said...



very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
IT training and job placement