REST Assured with plain/text response body

Carvia Tech | July 23, 2020 | 2 min read | 1,339 views


REST assured does not configure Parser for text/plain type of response by default, so you must enable support for this manually.

Gradle Setup

You can check the latest version of REST Assured library at Maven Repository

build.gradle
dependencies {
    testImplementation 'io.rest-assured:json-path:4.2.0'
    testImplementation 'io.rest-assured:xml-path:4.2.0'
    testImplementation 'io.rest-assured:kotlin-extensions:4.2.0'
    testImplementation 'io.rest-assured:spring-mock-mvc:4.2.0'
    testImplementation group: 'io.rest-assured', name: 'rest-assured', version: '4.2.0'
    testImplementation ('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

We will be using JUNIT 5 Jupyter Platform for running tests.

RestController Setup

We will create a simple controller method that returns text/plain response.

Controller that returns text/plain
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PingController {

    @GetMapping("/ping")
    public String pong() {
        return "pong";
    }
}

REST Assured Testcase

We will develop a simple test case that validates the full body text of GET /ping response.

REST Assured plain/text full body assertion
import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import org.junit.jupiter.api.Test;

import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.hamcrest.CoreMatchers.equalTo;

public class MockMvcTest {

    @Test
    public void testMockMvcGetPlainText() {
        RestAssured.registerParser("text/plain", Parser.TEXT);  (1)
        given().standaloneSetup(new PingController())
                .when().get("/ping")
                .then().assertThat()
                    .statusCode(200)
                    .body(equalTo("pong"));     (2)
    }
}
1 We are configuring REST Assured to handle text/plain type of response, which it does not handle by default.
2 We want to do full body content matching here, rather than partial json field matching.

Top articles in this category:
  1. Rest Assured API Testing Interview Questions
  2. REST Assured Basic Authentication
  3. RestAssured multipart file upload
  4. REST Assured vs Apache HttpClient and RestTemplate
  5. OAuth2 protected resources in RestAssured Testcases
  6. 50 SDET Java Interview Questions & Answers
  7. SDET: JUnit interview questions for automation engineer


Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info