SlideShare a Scribd company logo
1 of 27
Testing RESTful Webservices using the REST-assured Framework
Table of Contents ,[object Object]
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 ,[object Object]
Setting HTTP Headers
Verifying HTTP Header
Setting Cookies
Verifying Cookies
File Uploads
Registering Custom Parsers
Specification Reuse
Tutorial Sources
Prerequisites ,[object Object]
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 ,[object Object]
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>1.4</version>

More Related Content

What's hot

Test Design and Automation for REST API
Test Design and Automation for REST APITest Design and Automation for REST API
Test Design and Automation for REST APIIvan Katunou
 
introduction about REST API
introduction about REST APIintroduction about REST API
introduction about REST APIAmilaSilva13
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & DevelopmentAshok Pundit
 
An Introduction To Automated API Testing
An Introduction To Automated API TestingAn Introduction To Automated API Testing
An Introduction To Automated API TestingSauce Labs
 
API Test Automation
API Test Automation API Test Automation
API Test Automation SQALab
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServicesPrateek Tandon
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
API Testing: The heart of functional testing" with Bj Rollison
API Testing: The heart of functional testing" with Bj RollisonAPI Testing: The heart of functional testing" with Bj Rollison
API Testing: The heart of functional testing" with Bj RollisonTEST Huddle
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUDPrem Sanil
 
API Automation Testing Using RestAssured+Cucumber
API Automation Testing Using RestAssured+CucumberAPI Automation Testing Using RestAssured+Cucumber
API Automation Testing Using RestAssured+CucumberKnoldus Inc.
 

What's hot (20)

Api presentation
Api presentationApi presentation
Api presentation
 
Testing microservices with rest assured
Testing microservices with rest assuredTesting microservices with rest assured
Testing microservices with rest assured
 
Postman
PostmanPostman
Postman
 
Test Design and Automation for REST API
Test Design and Automation for REST APITest Design and Automation for REST API
Test Design and Automation for REST API
 
introduction about REST API
introduction about REST APIintroduction about REST API
introduction about REST API
 
Api Testing
Api TestingApi Testing
Api Testing
 
REST API Design & Development
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
 
An Introduction To Automated API Testing
An Introduction To Automated API TestingAn Introduction To Automated API Testing
An Introduction To Automated API Testing
 
POSTMAN.pptx
POSTMAN.pptxPOSTMAN.pptx
POSTMAN.pptx
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
API Test Automation
API Test Automation API Test Automation
API Test Automation
 
Rest & RESTful WebServices
Rest & RESTful WebServicesRest & RESTful WebServices
Rest & RESTful WebServices
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
API Testing: The heart of functional testing" with Bj Rollison
API Testing: The heart of functional testing" with Bj RollisonAPI Testing: The heart of functional testing" with Bj Rollison
API Testing: The heart of functional testing" with Bj Rollison
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
 
API Automation Testing Using RestAssured+Cucumber
API Automation Testing Using RestAssured+CucumberAPI Automation Testing Using RestAssured+Cucumber
API Automation Testing Using RestAssured+Cucumber
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
API Testing
API TestingAPI Testing
API Testing
 

Similar to Testing RESTful Webservices using the REST-assured framework

Introduction into Struts2 jQuery Grid Tags
Introduction into Struts2 jQuery Grid TagsIntroduction into Struts2 jQuery Grid Tags
Introduction into Struts2 jQuery Grid TagsJohannes Geppert
 
Spring MVC 3 Restful
Spring MVC 3 RestfulSpring MVC 3 Restful
Spring MVC 3 Restfulknight1128
 
Surfacing External Data Through Magnolia
Surfacing External Data Through MagnoliaSurfacing External Data Through Magnolia
Surfacing External Data Through MagnoliaSeanMcTex
 
Zend framework 05 - ajax, json and j query
Zend framework 05 - ajax, json and j queryZend framework 05 - ajax, json and j query
Zend framework 05 - ajax, json and j queryTricode (part of Dept)
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Pranav Prakash
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaJevgeni Kabanov
 
JavaServer Faces Anti-Patterns and Pitfalls
JavaServer Faces Anti-Patterns and PitfallsJavaServer Faces Anti-Patterns and Pitfalls
JavaServer Faces Anti-Patterns and PitfallsDennis Byrne
 
External Data Access with jQuery
External Data Access with jQueryExternal Data Access with jQuery
External Data Access with jQueryDoncho Minkov
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax componentsIgnacio Coloma
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentationipolevoy
 
Interoperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSInteroperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSCarol McDonald
 
Introduction to AJAX and DWR
Introduction to AJAX and DWRIntroduction to AJAX and DWR
Introduction to AJAX and DWRSweNz FixEd
 
Java Boilerplate Busters
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate BustersHamletDRC
 

Similar to Testing RESTful Webservices using the REST-assured framework (20)

Spring Capitulo 05
Spring Capitulo 05Spring Capitulo 05
Spring Capitulo 05
 
J s-o-n-120219575328402-3
J s-o-n-120219575328402-3J s-o-n-120219575328402-3
J s-o-n-120219575328402-3
 
Introduction into Struts2 jQuery Grid Tags
Introduction into Struts2 jQuery Grid TagsIntroduction into Struts2 jQuery Grid Tags
Introduction into Struts2 jQuery Grid Tags
 
My java file
My java fileMy java file
My java file
 
DevDays09 Internet Explorer 8
DevDays09 Internet Explorer 8DevDays09 Internet Explorer 8
DevDays09 Internet Explorer 8
 
Spring MVC 3 Restful
Spring MVC 3 RestfulSpring MVC 3 Restful
Spring MVC 3 Restful
 
Surfacing External Data Through Magnolia
Surfacing External Data Through MagnoliaSurfacing External Data Through Magnolia
Surfacing External Data Through Magnolia
 
Zend framework 05 - ajax, json and j query
Zend framework 05 - ajax, json and j queryZend framework 05 - ajax, json and j query
Zend framework 05 - ajax, json and j query
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
JavaServer Faces Anti-Patterns and Pitfalls
JavaServer Faces Anti-Patterns and PitfallsJavaServer Faces Anti-Patterns and Pitfalls
JavaServer Faces Anti-Patterns and Pitfalls
 
External Data Access with jQuery
External Data Access with jQueryExternal Data Access with jQuery
External Data Access with jQuery
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax components
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentation
 
Interoperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSInteroperable Web Services with JAX-WS
Interoperable Web Services with JAX-WS
 
Introduction to AJAX and DWR
Introduction to AJAX and DWRIntroduction to AJAX and DWR
Introduction to AJAX and DWR
 
WordPress APIs
WordPress APIsWordPress APIs
WordPress APIs
 
Java Boilerplate Busters
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate Busters
 

Recently uploaded

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 

Recently uploaded (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 

Testing RESTful Webservices using the REST-assured framework

  • 1. Testing RESTful Webservices using the REST-assured Framework
  • 2.
  • 10.
  • 19.
  • 22. The full tutorial and the RESTful Webservice to test can be found at my blog www.hascode.com
  • 23.
  • 30. Verify JSON via GET { &quot;email&quot;:&quot;test@hascode.com&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;test@hascode.com&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;test@hascode.com&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;test@hascode.com&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;test@hascode.com&quot;, &quot;firstName&quot;:&quot;Tim&quot;, &quot;lastName&quot;:&quot;Testerman&quot; },{ &quot;@id&quot;:&quot;20&quot;, &quot;email&quot;:&quot;dev@hascode.com&quot;, &quot;firstName&quot;:&quot;Sara&quot;, &quot;lastName&quot;:&quot;Stevens&quot; },{ &quot;@id&quot;:&quot;11&quot;, &quot;email&quot;:&quot;devnull@hascode.com&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;test@hascode.com&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>test@hascode.com</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;test@hascode.com&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>test@hascode.com</email> <firstName>Tim</firstName> <lastName>Testerman</lastName> </person> <person id=&quot;20&quot;> <email>dev@hascode.com</email> <firstName>Sara</firstName> <lastName>Stevens</lastName> </person> <person id=&quot;11&quot;> <email>devnull@hascode.com</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[.='test@hascode.com'] and firstName='Tim' and lastName='Testerman'&quot;), hasXPath(&quot;//person[@id='20']/email[.='dev@hascode.com'] and firstName='Sara' and lastName='Stevens'&quot;), hasXPath(&quot;//person[@id='1']/email[.='devnull@hascode.com'] 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>test@hascode.com</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;test@hascode.com&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;test@hascode.com&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;test@hascode.com&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.
  • 52. Clone with Mercurial: hg clone https://bitbucket.org/hascode/rest-assured-sample
  • 53.
  • 54. Please feel free to read further tutorials of mine on www.hascode.com
  • 55. I'm also on Twitter: http://twitter.com/hascode