Master REST-assured: Real‑World Spring Boot 3 API Testing Guide

Explore a rich Spring Boot 3 case collection while learning step‑by‑step how to use REST‑assured for API testing, covering dependency setup, GET/POST/PUT/DELETE requests, authentication, XML validation, file upload/download, and advanced features, with full code examples and a promise of ongoing updates.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master REST-assured: Real‑World Spring Boot 3 API Testing Guide

1. Introduction

This article presents a Spring Boot 3 practical case collection and provides a comprehensive tutorial on using the REST‑assured library to test and validate RESTful APIs.

2. Practical Examples

2.1 Dependency Management

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

2.2 Basic GET Request

Send a simple GET request and assert the response status and body values.

import static io.restassured.RestAssured.get;
import static org.hamcrest.Matchers.equalTo;

@Test
public void testApi() {
  get("http://localhost:8080/params/posts/1")
    .then()
    .statusCode(200)
    .body("type", equalTo("posts"))
    .body("id", equalTo(1));
}

2.3 POST Request

@Test
public void testPostApi() throws Throwable {
  JSON Object requestParams = new JSON Object();
  requestParams.put("title", "foo");
  requestParams.put("body", "bar");
  requestParams.put("userId", 1);
  given()
    .header("Content-Type", "application/json")
    .body(requestParams.toString())
    .when()
    .post("http://localhost:8080/params/create")
    .then()
    .statusCode(201)
    .body("title", equalTo("Spring Boot3全家桶实战案例锦集"))
    .body("name", equalTo("pack"))
    .body("uid", equalTo(1));
}

2.4 Adding Headers and Query Parameters

@Test
public void testHeaderAndParam() throws Throwable {
  given()
    .header("Accept", "application/json")
    .queryParam("userId", 1)
    .when()
    .get("http://localhost:8080/params/search")
    .then()
    .statusCode(200)
    .body("size()", greaterThan(0));
}

2.5 Basic Authentication

@Test
public void testAuth() {
  given()
    .auth().basic("root", "pack123")
    .when()
    .get("http://localhost:8080/params/login")
    .then()
    .statusCode(200);
}

2.6 Extracting Values from Response

@Test
public void testGiveValue() {
  Response response = get("http://localhost:8080/params/user/666");
  int userId = response.path("userId");
  String title = response.path("title");
  System.out.println("ID: " + userId);
  System.out.println("头衔: " + title);
}

2.7 XML Data Validation

@Test
public void testXml() {
  get("https://www.w3schools.com/xml/note.xml")
    .then()
    .statusCode(200)
    .body("note.to", equalTo("Tove"))
    .body("note.from", equalTo("Jani"));
}

The XML file used in the test:

Result of the XML validation test:

2.8 File Upload

@Test
public void testUpload() {
  File file = new File("d://Ollama.exe");
  given()
    .multiPart("file", file)
    .when()
    .post("http://localhost:8080/params/upload")
    .then()
    .statusCode(200);
}

2.9 File Download

@Test
public void testDownload() throws Throwable {
  InputStream inputStream = get("http://localhost:8080/resource/1.png").asInputStream();
  FileOutputStream outputStream = new FileOutputStream("1.png");
  byte[] buffer = new byte[1024];
  int bytesRead;
  while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead);
  }
  inputStream.close();
  outputStream.close();
}

2.10 DELETE & PUT Requests

// Delete user with ID 1
@Test
public void testDeleteUser() {
  delete("http://localhost:8080/users/1")
    .then()
    .statusCode(200);
}

// Update user information
@Test
public void testUpdateUser() throws Throwable {
  JSON Object requestParams = new JSON Object();
  requestParams.put("title", "pack");
  requestParams.put("body", "xxxooo");
  requestParams.put("userId", 1);
  given()
    .header("Content-Type", "application/json")
    .body(requestParams.toString())
    .when()
    .put("http://localhost:8080/users/1")
    .then()
    .statusCode(200)
    .body("title", equalTo("pack"))
    .body("body", equalTo("xxxooo"))
    .body("uid", equalTo(1));
}

By leveraging REST‑assured, developers can efficiently test and verify their APIs, ensuring reliable operation throughout development cycles.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentSpring BootAPI testingRest-Assured
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.