- Java 21
- Spring Boot 4.0 RC1
- Example Project: https://github.com/hantsy/spring7-sandbox/tree/master/boot-api-versioning-webflux
I tried to build a custom WebTestClient and run a test like this.
@WebFluxTest(controllers = GreetingController.class)
public class GreetingControllerTest {
WebTestClient webTestClient;
@Autowired GreetingController greetingController;
@BeforeEach
public void setup() {
this.webTestClient = WebTestClient
.bindToController(greetingController)
.apiVersioning(apiVersionConfigurer ->
apiVersionConfigurer.useRequestHeader("X-API-Version")
.setDefaultVersion("1.0")
)
.configureClient()
.apiVersionInserter(ApiVersionInserter.builder()
.useHeader("X-API-Version")
.build())
.build();
}
@Test
void testHello() {
this.webTestClient.get().uri("/hello")
// .apiVersion("1.0")
.exchange()
.expectBody(String.class).isEqualTo("Hello v1.0(Default)");
}
}
It will fail the tests. It seems the setDefaultVersion in the following block is not applied.
.apiVersioning(apiVersionConfigurer ->
apiVersionConfigurer.useRequestHeader("X-API-Version")
.setDefaultVersion("1.0")
)
But use a simple WebTestClientBuilderCustomizer to set the defaultApiVersion() on the Builder, it worked.
@TestConfiguration
static class TestConfig {
@Bean
WebTestClientBuilderCustomizer testClientBuilderCustomizer() {
return builder -> builder.defaultApiVersion("1.0")
.apiVersionInserter(ApiVersionInserter.useHeader("X-API-Version"))
.build();
}
}
@Autowired
WebTestClient webTestClient;