Rest-Assured Hello World - Getting started using simple example
Overview
In previous chapter we implemented REST web service to return either xml or JSON depending on some user parameter using ContentNegotiation parameter. In this chapter we test the same webservice using Rest-Assured.What is Rest-Assured ?
Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain. It eliminates the requirement of using boiler-plate code to test complex API responses, and supports both XML and JSON.Lets Begin
The pom.xml will be as follows-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>RestAssuredTest</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>pl.pragmatists</groupId> <artifactId>JUnitParams</artifactId> <version>1.0.4</version> </dependency> </dependencies> </project>Below we use the Given/When/Then structure that is borrowed from BDD (Behaviour Driven Development). In Given section we declare things like content type or request body. In When section we provide HTTP method and endpoint. In Then section we declare response verification.
package com.javainuse;
import org.hamcrest.Matchers;
import org.junit.Test;
import io.restassured.response.ValidatableResponse;
import static io.restassured.RestAssured.given;
public class TestRestAssured {
@Test
public void restAssuredTest() {
ValidatableResponse response =
given().queryParam("type", "json").when()
.get("http://localhost:8080/employee-management-system/viewEmployee/4").then();
System.out.println("Response is - "+response.extract().body().asString());
response.body(Matchers.containsString("emp4"));
}
}
Now start the REST web service we developed in the previous chapter. Run the above class and the test case gets
executed successfully. In this test the REST call is made and the response we check if it contains a particular string.
Download Source Code
Download it - Rest-Assured Hello WorldSee Also
REST Project to return XML Response REST Project to return JSON ResponsePopular Posts
1Z0-830 Java SE 21 Developer Certification
DP-600 Microsoft Fabric Analytics Engineer
SC-401 Microsoft Information Security Administrator
1Z0-819 Java SE 11 Developer Certification
1Z0-829 Java SE 17 Developer Certification
AWS AI Practitioner Certification
AZ-204 Azure Developer Associate Certification
AZ-305 Azure Solutions Architect Expert Certification
AZ-400 Azure DevOps Engineer Expert Certification
DP-100 Azure Data Scientist Associate Certification
AZ-900 Azure Fundamentals Certification
PL-300 Power BI Data Analyst Certification
Spring Professional Certification
Azure AI Foundry Hello World
Azure AI Agent Hello World
Foundry vs Hub Projects
Build Agents with SDK
Bing Web Search Agent
Function Calling Agent
Spring Boot + Azure Key Vault Hello World Example
Spring Boot + Elasticsearch + Azure Key Vault Example
Spring Boot Azure AD (Entra ID) OAuth 2.0 Authentication
Deploy Spring Boot App to Azure App Service
Secure Azure App Service using Azure API Management
Deploy Spring Boot JAR to Azure App Service
Deploy Spring Boot + MySQL to Azure App Service
Spring Boot + Azure Managed Identity Example
Secure Spring Boot Azure Web App with Managed Identity + App Registration
Elasticsearch 8 Security - Integrate Azure AD OIDC