Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringBootTest with RestClient.Builder that has base url + port already set. #40693

Closed
xenoterracide opened this issue May 10, 2024 · 1 comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@xenoterracide
Copy link
Contributor

xenoterracide commented May 10, 2024

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 10, 2024
@wilkinsona
Copy link
Member

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 spring-projects/spring-framework#31275. See also #40452 and #39848.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@wilkinsona wilkinsona added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

3 participants