diff --git a/module/spring-boot-webtestclient/src/main/java/org/springframework/boot/webtestclient/autoconfigure/AutoConfigureWebTestClient.java b/module/spring-boot-webtestclient/src/main/java/org/springframework/boot/webtestclient/autoconfigure/AutoConfigureWebTestClient.java index eef6aa7467c..1f05509822f 100644 --- a/module/spring-boot-webtestclient/src/main/java/org/springframework/boot/webtestclient/autoconfigure/AutoConfigureWebTestClient.java +++ b/module/spring-boot-webtestclient/src/main/java/org/springframework/boot/webtestclient/autoconfigure/AutoConfigureWebTestClient.java @@ -26,12 +26,14 @@ import java.time.Duration; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.test.context.PropertyMapping; +import org.springframework.boot.test.context.PropertyMapping.Skip; import org.springframework.test.web.reactive.server.WebTestClient; /** * Annotation that can be applied to a test class to enable a {@link WebTestClient}. * * @author Stephane Nicoll + * @author Jay Choi * @since 4.0.0 * @see WebTestClientAutoConfiguration */ @@ -48,6 +50,7 @@ public @interface AutoConfigureWebTestClient { * {@link Duration#parse(CharSequence)}). * @return the web client timeout */ + @PropertyMapping(skip = Skip.ON_DEFAULT_VALUE) String timeout() default ""; } diff --git a/module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webtestclient/autoconfigure/WebTestClientTimeoutSpringBootTestIntegrationTests.java b/module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webtestclient/autoconfigure/WebTestClientTimeoutSpringBootTestIntegrationTests.java new file mode 100644 index 00000000000..d1a3588de0a --- /dev/null +++ b/module/spring-boot-webtestclient/src/test/java/org/springframework/boot/webtestclient/autoconfigure/WebTestClientTimeoutSpringBootTestIntegrationTests.java @@ -0,0 +1,90 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.webtestclient.autoconfigure; + +import java.time.Duration; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.config.EnableWebFlux; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link SpringBootTest @SpringBootTest} with + * {@link AutoConfigureWebTestClient @AutoConfigureWebTestClient} timeout property + * binding. + * + * @author Jay Choi + * @author Andy Wilkinson + */ +@SpringBootTest(properties = { "spring.main.web-application-type=reactive" }, + classes = ExampleWebTestClientApplication.class) +class WebTestClientTimeoutSpringBootTestIntegrationTests { + + @Nested + @EnableWebFlux + @AutoConfigureWebTestClient + @TestPropertySource(properties = "spring.test.webtestclient.timeout=30s") + class TimeoutFromProperty { + + @Autowired + private WebTestClient webClient; + + @Test + void timeoutIsApplied() { + assertThat(this.webClient).hasFieldOrPropertyWithValue("responseTimeout", Duration.ofSeconds(30)); + } + + } + + @Nested + @EnableWebFlux + @AutoConfigureWebTestClient(timeout = "25s") + class TimeoutFromAnnotation { + + @Autowired + private WebTestClient webClient; + + @Test + void timeoutIsApplied() { + assertThat(this.webClient).hasFieldOrPropertyWithValue("responseTimeout", Duration.ofSeconds(25)); + } + + } + + @Nested + @EnableWebFlux + @AutoConfigureWebTestClient + class DefaultTimeout { + + @Autowired + private WebTestClient webClient; + + @Test + void timeoutIsApplied() { + assertThat(this.webClient).hasFieldOrPropertyWithValue("responseTimeout", Duration.ofSeconds(5)); + } + + } + +}