Skip to content

Assertions for WebClient, RestClient, and RestTemplate configurations #34524

Closed as not planned
@ttddyy

Description

@ttddyy

When testing configurations, it can be difficult to verify how WebClient, RestClient, and RestTemplate have been configured.

I’d like to suggest adding assertion (assertThat) support for these objects to simplify validation in tests.

Example Code:

@Test
void testWebClient() {
  new ApplicationContextRunner()
      .withConfiguration(AutoConfigurations.of(WebClientAutoConfiguration.class))
      .withUserConfiguration(MyConfig.class)
      .run(context -> {
        assertThat(context).hasNotFailed();
        WebClient.Builder builder = context.getBean(WebClient.Builder.class);
        assertThat(builder)  // or WebClient itself
            .hasBaseUrlSatisfying((baseUrl) -> assertThat(baseUrl).isEqualTo("http://localhost:8080/"))
            .hasFiltersSatisfying((filters) -> assertThat(filters).hasSize(1).first().isInstanceOf(MyExchangeFilterFunction.class))
            .hasNettyHttpClientSatisfying((httpClient) ->
              assertThat(httpClient)
                  .keepAliveEnabled()
                  .hasResponseTime((duration) -> assertThat(duration).hasSeconds(5)));
      });
}

@Configuration
static class MyConfig {

  @Bean
  WebClientCustomizer myCustomizer() {
    return (builder) ->
        builder.filter(new MyExchangeFilterFunction()).baseUrl("http://localhost:8080/");
  }

  @Bean
  ReactorNettyHttpClientMapper myReactorNettyHttpClientMapper() {
    return (httpClient) -> httpClient
        .keepAlive(true)
        .responseTimeout(Duration.ofSeconds(5));
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: testIssues in the test modulestatus: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions