REST Assured with plain/text response body

Upasana | July 23, 2020 | 2 min read | 1,654 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 Java Interview Questions for SDET Automation Engineer
  7. Junit interview questions for SDET automation engineer

Recommended books for interview preparation:

Find more on this topic: