Skip to content

Replace TestRestTemplate by RestTestClient using MockMvc #919

Description

@dennisvang

All acceptance tests use TestRestTemplate via WebIntegrationTest:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc
public abstract class WebIntegrationTest {

This requires a full web server configuration, as provided by SpringBootTest.WebEnvironment.DEFINED_PORT (or RANDOM_PORT).
However, for most tests this is overkill, and it is very slow.

Moreover, TestRestTemplate will be deprecated in spring-boot 4.2. Note that the underlying RestTemplate was already deprecated in spring-boot 4.0.0.

Instead of TestRestTemplate with full web server setup, we should consider using MockMvc (or MockMvcTester), together with the default SpringBootTest.WebEnvironment.MOCK.

An even better option is to use the new RestTestClient which supports both full server tests and mock tests:

RestTestClient is an HTTP client designed for testing server applications. It wraps Spring’s RestClient and uses it to perform requests, but exposes a testing facade for verifying responses. RestTestClient can be used to perform end-to-end HTTP tests. It can also be used to test Spring MVC applications without a running server via MockMvc.

related:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    choreMaintenancedevRelated to development, e.g. code style, refactoring, github workflows, etc.testsRelated to tests

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions