liveBook · Manning

RestTestClient

As explained in Testing Spring Boot Applications

RestTestClient is a Spring testing HTTP client for exercising MVC applications through simulated or real HTTP requests.

RestTestClient is Spring’s test-specific HTTP client for MVC applications. Introduced with Spring Framework 7 and Spring Boot 4, it follows the WebTestClient API while targeting the MVC stack. It can issue requests against mock servers or a running application over the network. section 4.2.3

Choose RestTestClient according to the web stack and test boundary:

  • Use it with @AutoConfigureRestTestClient in Spring Boot tests; its API is nearly identical to WebTestClient, so request and assertion structure stays consistent. section 4.2.3
  • Pair it with SpringBootTest.WebEnvironment.RANDOM_PORT when testing a full server, including error dispatch, session handling, configuration, logs, or multi-component communication. section 4.3 section 4.6
  • Prefer the lighter mock MVC approach for most API tests; MockMvcTester and RestTestClient are largely a stylistic choice when using that stack. section 4.2.3 section 4.6
  • Avoid introducing TestRestTemplate in new suites because it is scheduled for deprecation in Spring Framework 7.1 and Boot 4.2. section 4.2.3

Full-server MVC test

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureRestTestClient class FullServerTests { @Autowired RestTestClient testClient; @Test void displaysCustom404Page() { var response = testClient.get() .uri("/does-not-exist") .accept(MediaType.TEXT_HTML) .exchange(); var result = RestTestClientResponse.from(response); assertThat(result).hasStatus(HttpStatus.NOT_FOUND) .bodyText() .containsIgnoringCase("404 NOT FOUND"); } } Spring Boot assigns the client the running server’s base URL, so the test does not manage the random port directly. section 4.3

How Testing Spring Boot Applications covers it

How deep it goes
Defines the client’s stack, API compatibility, and test boundaries while deferring detailed usage beyond a focused example.
Examples
Demonstrates an HTML 404 assertion against a random-port application and a mock-server weather-page response check.
What you can do after
Apply it via Spring Boot autoconfiguration to test MVC responses, bodies, error pages, and full-server behavior.
How it connects
Connects MVC, WebFlux, WebTestClient, MockMvcTester, RestClient, TestRestTemplate, and random-port Spring Boot tests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.