Posts

Showing posts with the label @RestClientTest

Spring Boot - Testing REST Clients With @RestClientTest - Example

Image
In this section, we will learn how to test  REST clients with @RestClientTest in Spring Boot application. The Spring Framework provides the following Rest clients for making calls to REST endpoints: RestTemplate  RestClient (Supporting from Spring 6.1 onwards) WebClient HTTP Interface In this example we are using RestTemplate client for  making call to REST endpoint .  We will implement a service that will fetch data from the JSONPlaceholder API.  As the part of testing we are using  MockRestServiceServer  to mock real API. It eliminates the use of an actual server and thus speeds up the testing process. 1.  @RestClientTest Instead of bootstrapping the entire application context for every test, @RestClientTest initializes the Spring application context with only those beans needed to test REST Clients. It will auto-configure Jackson , GSON , Jsonb , configures a RestTemplateBuilder , and adds support for MockRestServiceServer . The auto-configuration that @RestClientTest import by def