2

I have developed a Spring Boot app in version 2.7.x. For the integration tests i am trying to customize the WebTestClient to add a default header.

I have already tried this but the header is not added in the request:

@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestClass1{

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void test1() throws Exception {
        webTestClient.get()
                .uri("/foobar/")
                .exchange()
                .expectStatus().isOk()
                .expectBody(JsonArray.class);
   }
}

Configuration class:

@TestConfiguration
public class WebTestClientConfig {

    @Bean
    public WebTestClient client() {
        return WebTestClient.bindToServer()
                .responseTimeout(Duration.ofMinutes(2))
                .defaultHeader(API_KEY_HEADER, API_KEY_VALUE)
                .exchangeStrategies(ExchangeStrategies.withDefaults())
                .build();
    }
}

Update:

I also tried to use the WebTestClientBuilderCustomizer in the 'WebTestClientConfig' class and removed the 'client()' method but the problem still exists. See this example:

@TestConfiguration
public class WebTestClientConfig {

    @Bean
    public WebTestClientBuilderCustomizer webTestClientBuilderCustomizer() {
        return (builder) -> builder.defaultHeader(API_KEY_HEADER, API_KEY_VALUE);
    }

}

Any ideas on how to archive my goal? Thx.

Ben
  • 290
  • 2
  • 17
  • 1
    Did you check https://stackoverflow.com/questions/50607285/spring-boot-testconfiguration-not-overriding-bean-during-integration-test? I suspect `WebTestClientConfig` is not effective. – samabcde Apr 11 '23 at 15:02
  • Thank you very much for the hint. I have tested both annotations: @Import(WebTestClient.class) and @ContextConfiguration(classes = WebTestClient.class), but without success. I get this error message on startup: 'ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.'. – Ben Apr 11 '23 at 15:30
  • 1
    I can't reproduce the error you see, take a look on https://stackoverflow.com/questions/50231736/applicationcontextexception-unable-to-start-servletwebserverapplicationcontext – samabcde Apr 11 '23 at 15:35
  • It works after all! :-) I had a spelling mistake in the class name. Thanks for your help. – Ben Apr 11 '23 at 16:29

1 Answers1

1

With the help of @samabcde I changed my code to this solution:

Test class:

@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import(WebTestClientConfig.class) // <---- !added!
public class TestClass1{

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void test1() throws Exception {
        webTestClient.get()
                .uri("/foobar/")
                .exchange()
                .expectStatus().isOk()
                .expectBody(JsonArray.class);
   }
}

Configuration class:

@TestConfiguration
public class WebTestClientConfig {

    @Bean
    public WebTestClientBuilderCustomizer webTestClientBuilderCustomizer() {
        return (builder) -> builder.defaultHeader(API_KEY_HEADER, API_KEY_VALUE);
    }

}
Ben
  • 290
  • 2
  • 17