Introduction to API Automation with Rest Assured

Setup and Configuration

Learning Outcome

3

Set up a Rest Assured project using Java and required dependencies.

2

Understand what Rest Assured is and why it is widely used for automating REST APIs.

1

Understand the role of API automation in modern software testing.

Recall

What is API Testing?

  • API Testing is a type of software testing that verifies whether Application Programming Interfaces (APIs) are working correctly.
  •  It focuses on validating the requests, responses, data accuracy, and business logic between different software systems without interacting with the user interface.
  • In simple terms, API testing checks whether systems communicate with each other properly and return the expected results.

Think about a package delivery service.

When an order is placed:

In API communication:

  • Client → Sends request
  • Server → Processes request
  • Response → Data returned

In software testing:

  • Applications communicate with each other using APIs.

  • Instead of manually checking these communications, testers create automation scripts.

Rest Assured works like the automation assistant — it automatically sends API requests, checks responses, and ensures everything works as expected

Why API Automation is Important??

  • Automating API tests helps teams validate backend functionality quickly and efficiently.
  • Since APIs operate behind the UI, testing them early helps identify issues before they affect the user interface.

Benefits of API automation include:

  • Faster execution compared to manual testing

  • Early detection of defects in the backend layer
    Easy integration with CI/CD pipelines

  • Ability to test large numbers of API scenarios repeatedly

Transition to concept

In the previous section, we understood what API testing is and why it is important.

Postman or Swagger help us manually send requests and validate responses

 Manual testing can become time-consuming

In real-world projects, APIs need to be tested frequently, repeatedly, and often as part of automated pipelines.

Transition to Concept

  • One of the most widely used tools for automating REST API testing in Java is Rest Assured.

This is where API automation tools become essential.

Manual API TestingAutomation API Testing
Performed using tools like Postman or SwaggerUses automation tools and frameworks
Requires manual execution of requestsTests run automatically with scripts
Suitable for quick validation and explorationIdeal for regression testing and large test suites
Not efficient for repetitive testingSupports continuous integration and continuous testing

What is REST Assured?

REST Assured is a Java-based API automation testing framework designed specifically for testing RESTful APIs

Built on top of Java HTTP clients like Apache HttpClient, it integrates with popular testing frameworks

What is REST Assured?

  • Fluent and readable syntax: Write tests that are easy to understand and maintain
  • JSON/XML assertions: Validate response data in various formats with ease.
  • Functional and integration testing: Versatile framework for multiple testing scenarios.

Why use REST Assured?

  1. Java Developer Friendly: Easy to learn with a familiar syntax for Java developers.

2. BDD-Style Syntax: Write tests that read like natural language specifications

4. CI/CD Integration: Seamlessly works with continuous integration pipelines

3. Method Chaining: Create readable test flows with intuitive method chains.

Maven Project Setup

1. Prepare Tools

  • Install Java JDK 8+, Eclipse/IntelliJ IDEA and Apache Maven.

2. Create Project

  • Open IDE and create a new Maven project

3. Configure Details

  • Set Group ID and Artifact ID for your project

4. Structure Folders

  • Organize with src/main/java and src/test/java

    folders.

Add Dependencies in pom.xml

io.rest-assured
rest-assured
5.3.0
test

org.testng
testng
7.8.0
test

Add these dependencies to your pom.xml file inside the dependencies block. Always use the latest stable versions from Maven Central

Build and Validate Project

Save pom.xml

  • Maven will automatically download dependencies.

Verify Libraries

  • Check External/Referenced Libraries in your IDE.

Run Sample Test

  • Execute a simple test to confirm proper setup.
import static io.restassured.RestAssured.*;
import org.testng.annotations.Test;
public class SampleTest {
 @Test
 public void testStatusCode() {
 get("https://jsonplaceholder.typicode.com/posts/1")
  .then()
  .statusCode(200);
  }
  }

Choosing between TestNG and JUnit

TestNG BenefitsJUnit Benefits
Parallel test execution for faster feedbackWidely adopted in open-source projects
Advanced listeners for custom reportingSimple, lightweight architecture
Test grouping for better organizationExcellent IDE integration
Dependency management between testsFamiliar to most Java developers

Both frameworks are fully supported by REST Assured.

Choose based on your project's specific requirements.

Recommended Project Structure

This structure ensures clean, maintainable test architecture as your project grows.

/src
/main
/java (Utility Classes)
/test
/java
/tests
- SampleTest.java
/base
- BaseTest.java
/utils
- JsonUtil.java
- ConfigReader.java

Contains actual test classes with API verifications.

Helper classes for JSON parsing and token generation

Shared setup and configuration classes.

Best Practices for Setup

1. Base Configuration

  • Keep base URI and common headers in a shared base class. This centralizes common settings.

2. Environment Properties

  • Use properties files for environment-specific configs. Enables easy switching between test environments.

3. Modular Design

  • Create reusable test components. Use Hamcrest for readable assertions.

Summary

4

Rest Assured automates API testing, ensure reliable backend functionality.

3

Manual testing suits quick checks;automation excels ,large-scale testing.

2

API automation speeds testing,catches issues early, supports continuous testing.

1

API testing validates system communication via requests, responses, and data.

Quiz

Which of the following is a benefit of API automation?

A. Slower execution

B. Requires more manual effort

C. Faster and repeatable testing

D. Only used for UI testing

Quiz-answer

Which of the following is a benefit of API automation?

A. Slower execution

B. Requires more manual effort

C. Faster and repeatable testing

D. Only used for UI testing

Introduction to API Automation with Rest Assured: Setup and Configuration

By Content ITV

Introduction to API Automation with Rest Assured: Setup and Configuration

  • 106