liveBook · Manning

@RestClientTest

As explained in Testing Spring Boot Applications

@RestClientTest is a Spring Boot test annotation that configures a focused test context for clients calling external HTTP services.

@RestClientTest is a Spring Boot test slice for code that calls external HTTP services through RestClient. It loads the selected client-facing components and HTTP-client testing infrastructure instead of a full application context. Add spring-boot-starter-restclient-test as a test-scoped dependency. section 5.4

Name the components under test explicitly: slice scanning cannot infer which beans need testing from a RestClient.Builder constructor parameter. The slice supplies a MockRestServiceServer and a RestClientCustomizer that connects the default Boot client to that server. If the selected component needs other services, provide them through test configuration, @MockitoBean, or @TestBean; they are not loaded automatically. section 5.4

Listing 5.7 Using the @RestClientTest annotation.
@RestClientTest(components = { OpenMeteoWeatherDataService.class }) #1
class OpenMeteoWeatherDataServiceTest {

	@Autowired
	private OpenMeteoWeatherDataService service; #2

	@Autowired
	private MockRestServiceServer mockServer; #3

	// ... tests ...

}

A focused test names its service in @RestClientTest, then injects both that service and MockRestServiceServer. section 5.4

Test a weather-service request

Set a mock-server expectation for the latitude and longitude query parameters, respond with JSON, then call the service and assert its mapped weather values. A separate test can return HTTP 400 and assert that the service throws IllegalArgumentException. Request matchers and response creators can also be customized; mockServer.verify() checks whether configured expectations were met. section 5.4

Choose this slice when the HTTP integration is the subject of the test. A full-context test is an alternative when broader application wiring matters. Slices can give focused feedback, but accumulating too many distinct slices risks context thrashing. section 5.8

How Testing Spring Boot Applications covers it

How deep it goes
Explains component selection, automatic mock-server wiring, dependency replacement, request expectations, responses, and verification; does not develop full-context testing.
Examples
Uses a weather-service test with query-parameter expectations, JSON responses, an HTTP 400 error case, and mock-server verification.
What it assumes
Assumes a Spring service using RestClient.Builder; explains slice setup and mock-server wiring rather than teaching HTTP client construction from zero.
What you can do after
Apply it via @RestClientTest to configure canned HTTP responses and assert a selected client's behavior.
How it connects
Connects RestClient.Builder, MockRestServiceServer, RestClientCustomizer, Spring Boot test slices, @MockitoBean, and @TestBean.

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.