I went scavenging the documentation couldn't find an annotation to configure RestClient.Builder although it does autowire it...
this is what I tried
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class AuthorizationServerTest {
@Autowired
RestClient.Builder restClientBuilder;
@Test
void noAuth() {
var restClient = restClientBuilder
.messageConverters(converters -> converters.addFirst(new OAuth2AccessTokenResponseHttpMessageConverter()))
.build();
I expected it to be equivalent to
var restClient = RestClient.builder()
// .requestFactory(new HttpComponentsClientHttpRequestFactory()) not this because I'm doing this for tracing because of https://github.com/spring-projects/spring-framework/issues/32783 which might be better solved by Spring Boot
.baseUrl("http://localhost:" + this.port)
.messageConverters(converters -> converters)
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "/authorize": Target host is not specified
Running with Spring Boot v3.2.5, Spring v6.1.6
Comment From: wilkinsona
There's no test-specific support for RestClient. What's being injected into the test class is the "main" RestClient.Builder that's auto-configured by RestClientAutoConfiguration. We can't change it in a test-specific way as it may break any consumers in main application code.
Support for a TestRestClient, which I'm guess may be what you're looking for, is being tracked by https://github.com/spring-projects/spring-framework/issues/31275. See also https://github.com/spring-projects/spring-boot/issues/40452 and https://github.com/spring-projects/spring-boot/issues/39848.