ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Writing First REST Assured Test

author
Generated by
Hitendra Singhal

26/10/2024

API Testing

Sign in to read full article

When it comes to testing web services, particularly RESTful APIs, REST Assured is an industry favorite. This Java library provides a domain-specific language (DSL) for writing tests in a readable and expressive manner. Let's dive into how to set up your first REST Assured test and get you on the path to validating your API endpoints.

Prerequisites

Before we start coding, make sure you have the following set up:

  1. Java Development Kit (JDK) – Download and install the latest JDK on your machine.
  2. Maven – You will need Maven to manage your project dependencies. Ensure it's installed and configured correctly.
  3. IDE – Any Java-compatible IDE will work, but IntelliJ IDEA and Eclipse are popular choices.

Setting Up Your Project

  1. Create a Maven Project: Open your IDE, create a new Maven project, and set the project coordinates:

    <groupId>com.example</groupId> <artifactId>rest-assured-tests</artifactId> <version>1.0-SNAPSHOT</version>
  2. Add REST Assured Dependency: In the pom.xml file, include the REST Assured dependency. This will allow you to use its features in your tests. Add the following snippet inside the <dependencies> tag:

    <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.3.0</version> <scope>test</scope> </dependency>

    Be sure to check the REST Assured documentation for the latest version.

Writing Your First Test

To illustrate how to write a REST Assured test, let’s consider testing a simple API endpoint. Assume we want to test a hypothetical GET request at https://api.example.com/users/1, where it is expected to return a user object.

  1. Create a Test Class: In your src/test/java directory, create a class named UserApiTests.java.

  2. Write the Test Method: Here’s how you can write a simple test for the API:

    import io.restassured.RestAssured; import io.restassured.response.Response; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class UserApiTests { @Test public void getUserById() { // Specify the base URI RestAssured.baseURI = "https://api.example.com"; // Send a GET request and validate the response given() .pathParam("id", 1) // Set path parameter .when() .get("/users/{id}") .then() .statusCode(200) // Check for 200 OK response .body("id", equalTo(1)) // Validate the response body .body("name", notNullValue()); // Ensure 'name' is present } }

Explanation of the Code

  • Imports: We import necessary classes from REST Assured for making requests and validating responses, as well as JUnit for defining our test case.

  • Base URI: The baseURI sets the root URL of the API. All subsequent requests will use this as a prefix.

  • Given-When-Then: This structure is a common paradigm in behavior-driven development:

    • Given: Setup the request. Here, we define a path parameter for the user ID.
    • When: Execute the GET request to the specified endpoint.
    • Then: Assert the expected outcomes—status code and response content.

Running the Test

To execute your test, you can use a command line within your project directory:

mvn test

If everything is set up correctly, you should see a successful test result indicating that your API is returning the expected data.

Conclusion

Now that you've walked through writing your first REST Assured test, you have a solid foundation for testing RESTful APIs. Feel free to expand on this by adding more methods to explore different endpoints or using other HTTP methods, such as POST, PUT, and DELETE.

This is just the beginning! Happy testing!

Popular tags

API TestingREST AssuredAutomated Testing

Share now!

Like & bookmark

Related collections

  • Comprehensive API Testing: From Basics to Automation

    18/09/2024 · API Testing

  • REST Assured: Advanced API Testing

    26/10/2024 · API Testing

  • Mastering API Testing with Postman

    21/09/2024 · API Testing

Related articles

  • Authentication Techniques in API Testing

    18/09/2024 · API Testing

  • API Chaining and Dependencies in REST Assured

    26/10/2024 · API Testing

  • Data Driven Testing with REST Assured

    26/10/2024 · API Testing

  • Understanding REST Assured Basic Syntax and Structure for API Testing

    26/10/2024 · API Testing

  • Managing Test Data in API Testing

    18/09/2024 · API Testing

  • Best Practices for Effective API Testing

    18/09/2024 · API Testing

  • Error Handling and Negative Testing in APIs

    18/09/2024 · API Testing

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design