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
