SlideShare a Scribd company logo
Testing RESTful Webservices using the REST-assured Framework
Table of Contents Prerequisites
REST-assured and Maven
Verify JSON via GET
JsonPath
Groovy Closures – The JSON
Groovy Closures – The Test
Verifying XML, Xpath, Schema
Request Parameters
Status Codes, Headers Handling Basic Authentication
Setting HTTP Headers
Verifying HTTP Header
Setting Cookies
Verifying Cookies
File Uploads
Registering Custom Parsers
Specification Reuse
Tutorial Sources
Prerequisites We’re going to need a JDK and Maven .. nothing more …
Java Development Kit >= 6
Maven 3
The full tutorial and the RESTful Webservice to test can be found at my  blog www.hascode.com
REST-assured and Maven Only one dependency needed:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>1.4</version>

More Related Content

PPTX
Rest assured
Varun Deshpande
 
PPTX
Test in Rest. API testing with the help of Rest Assured.
Artem Korchevyi
 
PDF
2015-StarWest presentation on REST-assured
Eing Ong
 
PPTX
Automation Testing
AbdulImrankhan7
 
PPTX
B4USolution_API-Testing
b4usolution .
 
PDF
How to Automate API Testing
Bruno Pedro
 
PDF
Rest api 테스트 수행가이드
SangIn Choung
 
PDF
API Testing. Streamline your testing process.
Andrey Oleynik
 
Rest assured
Varun Deshpande
 
Test in Rest. API testing with the help of Rest Assured.
Artem Korchevyi
 
2015-StarWest presentation on REST-assured
Eing Ong
 
Automation Testing
AbdulImrankhan7
 
B4USolution_API-Testing
b4usolution .
 
How to Automate API Testing
Bruno Pedro
 
Rest api 테스트 수행가이드
SangIn Choung
 
API Testing. Streamline your testing process.
Andrey Oleynik
 

What's hot (20)

PPTX
Api Testing
Vishwanath KC
 
PPTX
Automation testing
Mona M. Abd El-Rahman
 
PPTX
BDD Approach with Karate Framework in Service Tests
kloia
 
PPTX
Test your microservices with REST-Assured
Michel Schudel
 
PDF
An Introduction To Automated API Testing
Sauce Labs
 
PDF
De a máxima cobertura nos seus testes de API
Elias Nogueira
 
PPTX
API Testing Using REST Assured with TestNG
Siddharth Sharma
 
PPT
Postman.ppt
ParrotBAD
 
PPT
Performance testing with Jmeter
Prashanth Kumar
 
PDF
Karate - powerful and simple framework for REST API automation testing
Roman Liubun
 
PPTX
Api testing
Keshav Kashyap
 
PDF
Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드
SangIn Choung
 
PDF
REST API and CRUD
Prem Sanil
 
PDF
Postman Webinar: Postman 101
Nikita Sharma
 
PDF
Postman: An Introduction for Testers
Postman
 
PDF
API Testing
Bikash Sharma
 
PPT
Ppt of soap ui
pkslide28
 
PDF
Test Automation
rockoder
 
PPTX
Introduction to performance testing
Richard Bishop
 
PPT
Test automation process
Bharathi Krishnamurthi
 
Api Testing
Vishwanath KC
 
Automation testing
Mona M. Abd El-Rahman
 
BDD Approach with Karate Framework in Service Tests
kloia
 
Test your microservices with REST-Assured
Michel Schudel
 
An Introduction To Automated API Testing
Sauce Labs
 
De a máxima cobertura nos seus testes de API
Elias Nogueira
 
API Testing Using REST Assured with TestNG
Siddharth Sharma
 
Postman.ppt
ParrotBAD
 
Performance testing with Jmeter
Prashanth Kumar
 
Karate - powerful and simple framework for REST API automation testing
Roman Liubun
 
Api testing
Keshav Kashyap
 
Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드
SangIn Choung
 
REST API and CRUD
Prem Sanil
 
Postman Webinar: Postman 101
Nikita Sharma
 
Postman: An Introduction for Testers
Postman
 
API Testing
Bikash Sharma
 
Ppt of soap ui
pkslide28
 
Test Automation
rockoder
 
Introduction to performance testing
Richard Bishop
 
Test automation process
Bharathi Krishnamurthi
 
Ad

Recently uploaded (20)

PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Ad

Testing RESTful Webservices using the REST-assured framework

  • 1. Testing RESTful Webservices using the REST-assured Framework
  • 2. Table of Contents Prerequisites
  • 10. Status Codes, Headers Handling Basic Authentication
  • 19. Prerequisites We’re going to need a JDK and Maven .. nothing more …
  • 22. The full tutorial and the RESTful Webservice to test can be found at my blog www.hascode.com
  • 23. REST-assured and Maven Only one dependency needed:
  • 30. Verify JSON via GET { &quot;email&quot;:&quot;[email protected]&quot;, &quot;firstName&quot;:&quot;Tim&quot;, &quot;id&quot;:&quot;1&quot;, &quot;lastName&quot;:&quot;Testerman&quot; } @Test public void testGetSingleUser() { expect(). statusCode(200). body( &quot;email&quot;, equalTo(&quot;[email protected]&quot;), &quot;firstName&quot;, equalTo(&quot;Tim&quot;), &quot;lastName&quot;, equalTo(&quot;Testerman&quot;), &quot;id&quot;, equalTo(&quot;1&quot;)). when(). get(&quot;/service/single-user&quot;); }
  • 31. JsonPath { &quot;email&quot;:&quot;[email protected]&quot;, &quot;firstName&quot;:&quot;Tim&quot;, &quot;id&quot;:&quot;1&quot;, &quot;lastName&quot;:&quot;Testerman&quot; } @Test public void testGetSingleUserProgrammatic() { Response res = get(&quot;/service/single-user&quot;); assertEquals(200, res.getStatusCode()); String json = res.asString(); JsonPath jp = new JsonPath(json); assertEquals(&quot;[email protected]&quot;, jp.get(&quot;email&quot;)); assertEquals(&quot;Tim&quot;, jp.get(&quot;firstName&quot;)); assertEquals(&quot;Testerman&quot;, jp.get(&quot;lastName&quot;)); assertEquals(&quot;1&quot;, jp.get(&quot;id&quot;)); }
  • 32. Groovy Closures – The JSON { &quot;person&quot;:[ { &quot;@id&quot;:&quot;1&quot;, &quot;email&quot;:&quot;[email protected]&quot;, &quot;firstName&quot;:&quot;Tim&quot;, &quot;lastName&quot;:&quot;Testerman&quot; },{ &quot;@id&quot;:&quot;20&quot;, &quot;email&quot;:&quot;[email protected]&quot;, &quot;firstName&quot;:&quot;Sara&quot;, &quot;lastName&quot;:&quot;Stevens&quot; },{ &quot;@id&quot;:&quot;11&quot;, &quot;email&quot;:&quot;[email protected]&quot;, &quot;firstName&quot;:&quot;Mark&quot;, &quot;lastName&quot;:&quot;Mustache&quot; } ] }
  • 33. Groovy Closures – The Test @Test public void testFindUsingGroovyClosure() { String json = get(&quot;/service/persons/json&quot;).asString(); JsonPath jp = new JsonPath(json); jp.setRoot(&quot;person&quot;); Map person = jp.get(&quot;find {e -> e.email =~ /test@/}&quot;); assertEquals(&quot;[email protected]&quot;, person.get(&quot;email&quot;)); assertEquals(&quot;Tim&quot;, person.get(&quot;firstName&quot;)); assertEquals(&quot;Testerman&quot;, person.get(&quot;lastName&quot;)); }
  • 34. Verifying XML – The XML <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?> <user> <email>[email protected]</email> <firstName>Tim</firstName> <id>1</id> <lastName>Testerman</lastName> </user>
  • 35. Verifying XML – The Test @Test public void testGetSingleUserAsXml() { expect(). statusCode(200). body( &quot;user.email&quot;, equalTo(&quot;[email protected]&quot;), &quot;user.firstName&quot;, equalTo(&quot;Tim&quot;), &quot;user.lastName&quot;, equalTo(&quot;Testerman&quot;), &quot;user.id&quot;, equalTo(&quot;1&quot;)). when(). get(&quot;/service/single-user/xml&quot;); }
  • 36. Verifying using Xpath – The XML <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?> <people> <person id=&quot;1&quot;> <email>[email protected]</email> <firstName>Tim</firstName> <lastName>Testerman</lastName> </person> <person id=&quot;20&quot;> <email>[email protected]</email> <firstName>Sara</firstName> <lastName>Stevens</lastName> </person> <person id=&quot;11&quot;> <email>[email protected]</email> <firstName>Mark</firstName> <lastName>Mustache</lastName> </person> </people>
  • 37. Verifying using XPath – the Test @Test public void testGetPersons() { expect(). statusCode(200). body( hasXPath(&quot;//person[@id='1']/email[.='[email protected]'] and firstName='Tim' and lastName='Testerman'&quot;), hasXPath(&quot;//person[@id='20']/email[.='[email protected]'] and firstName='Sara' and lastName='Stevens'&quot;), hasXPath(&quot;//person[@id='1']/email[.='[email protected]'] and firstName='Mark' and lastName='Mustache'&quot;)). when(). get(&quot;/service/persons/xml&quot;); }
  • 38. Verify using Schema – The XML <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?> <user> <email>[email protected]</email> <firstName>Tim</firstName> <id>1</id> <lastName>Testerman</lastName> </user>
  • 39. Verifying using Schema – The XSD <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <schema xmlns=&quot;http://www.w3.org/2001/XMLSchema&quot;> <element name=&quot;user&quot;> <complexType> <sequence> <element name=&quot;email&quot;> <simpleType> <restriction base=&quot;string&quot;> <pattern value=&quot;.+@.+&quot;></pattern> </restriction> </simpleType> </element> <element name=&quot;firstName&quot; type=&quot;string&quot;></element> <element name=&quot;id&quot; type=&quot;int&quot;></element> <element name=&quot;lastName&quot; type=&quot;string&quot;></element> </sequence> </complexType> </element> </schema>
  • 40. Verifying using Schema – The Test @Test public void testGetSingleUserAgainstSchema() { InputStream xsd = getClass().getResourceAsStream(&quot;/user.xsd&quot;); assertNotNull(xsd); expect(). statusCode(200). body( matchesXsd(xsd)). when(). get(&quot;/service/single-user/xml&quot;); }
  • 41. Handling Request Parameters @Test public void testCreateuser() { final String email = &quot;[email protected]&quot;; final String firstName = &quot;Tim&quot;; final String lastName = &quot;Tester&quot;; given(). parameters( &quot;email&quot;, email, &quot;firstName&quot;, firstName, &quot;lastName&quot;, lastName). expect(). body(&quot;email&quot;, equalTo(email)). body(&quot;firstName&quot;, equalTo(firstName)). body(&quot;lastName&quot;, equalTo(lastName)). when(). get(&quot;/service/user/create&quot;); }
  • 42. Verifying HTTP Status Codes @Test public void testStatusNotFound() { expect(). statusCode(404). when(). get(&quot;/service/status/notfound&quot;); }
  • 43. Handling Basic Authentication @Test public void testAuthenticationWorking() { // we're not authenticated, service returns &quot;401 Unauthorized&quot; expect(). statusCode(401). when(). get(&quot;/service/secure/person&quot;); // with authentication it is working expect(). statusCode(200). when(). with(). authentication().basic(&quot;admin&quot;, &quot;admin&quot;). get(&quot;/service/secure/person&quot;); }
  • 44. Setting HTTP Headers @Test public void testSetRequestHeaders() { expect(). body(equalTo(&quot;TEST&quot;)). when(). with(). header(&quot;myparam&quot;, &quot;TEST&quot;). get(&quot;/service/header/print&quot;); expect(). body(equalTo(&quot;foo&quot;)). when(). with(). header(&quot;myparam&quot;, &quot;foo&quot;). get(&quot;/service/header/print&quot;); }
  • 45. Verifying HTTP Header @Test public void testReturnedHeaders() { expect(). headers(&quot;customHeader1&quot;, &quot;foo&quot;, &quot;anotherHeader&quot;, &quot;bar&quot;). when(). get(&quot;/service/header/multiple&quot;); }
  • 46. Setting Cookies @Test public void testAccessSecuredByCookie() { expect(). statusCode(403). when(). get(&quot;/service/access/cookie-token-secured&quot;); given(). cookie(&quot;authtoken&quot;, &quot;abcdef&quot;). expect(). statusCode(200). when(). get(&quot;/service/access/cookie-token-secured&quot;); }
  • 47. Verifying Cookies @Test public void testModifyCookie() { expect(). cookie(&quot;userName&quot;, equalTo(&quot;Ted&quot;)). when(). with().param(&quot;name&quot;, &quot;Ted&quot;). get(&quot;/service/cookie/modify&quot;); expect(). cookie(&quot;userName&quot;, equalTo(&quot;Bill&quot;)). when(). with().param(&quot;name&quot;, &quot;Bill&quot;). get(&quot;/service/cookie/modify&quot;); }
  • 48. File Uploads @Test public void testFileUpload() { final File file = new File(getClass().getClassLoader() .getResource(&quot;test.txt&quot;).getFile()); assertNotNull(file); assertTrue(file.canRead()); given(). multiPart(file). expect(). body(equalTo(&quot;This is an uploaded test file.&quot;)). when(). post(&quot;/service/file/upload&quot;); }
  • 49. Registering Custom Parsers @Test public void testRegisterParserForUnknownContentType() { RestAssured.registerParser(&quot;text/json&quot;, Parser.JSON); expect(). body(&quot;test&quot;, equalTo(true)). when(). get(&quot;/service/detail/json&quot;); }
  • 50. Specification Reuse @Test public void testSpecReuse() { ResponseSpecBuilder builder = new ResponseSpecBuilder(); builder.expectStatusCode(200); builder.expectBody(&quot;email&quot;, equalTo(&quot;[email protected]&quot;)); builder.expectBody(&quot;firstName&quot;, equalTo(&quot;Tim&quot;)); builder.expectBody(&quot;lastName&quot;, equalTo(&quot;Testerman&quot;)); builder.expectBody(&quot;id&quot;, equalTo(&quot;1&quot;)); ResponseSpecification responseSpec = builder.build(); // now we're able to use this specification for this test expect(). spec(responseSpec). when(). get(&quot;/service/single-user&quot;); // now re-use for another test that returns similar data .. you may // extend the specification with further tests as you wish final String email = &quot;[email protected]&quot;; final String firstName = &quot;Tim&quot;; final String lastName = &quot;Testerman&quot;; expect(). spec(responseSpec). when(). with(). parameters( &quot;email&quot;, email, &quot;firstName&quot;, firstName, &quot;lastName&quot;,lastName). get(&quot;/service/user/create&quot;); }
  • 51. Tutorial Sources On Bitbucket: http://bitbucket.org/hascode
  • 52. Clone with Mercurial: hg clone https://bitbucket.org/hascode/rest-assured-sample
  • 53. The End Thank you for your audience :)
  • 54. Please feel free to read further tutorials of mine on www.hascode.com
  • 55. I'm also on Twitter: http://twitter.com/hascode