From f3fa952cbc1911e9f0685820be22c2df0684361c Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Mon, 27 Aug 2018 16:35:49 -0700 Subject: [PATCH] Support WebExceptionHandler in @WebFluxTest Closes gh-13627 --- .../reactive/WebFluxTypeExcludeFilter.java | 2 + .../webclient/ExampleController1.java | 5 +++ .../webclient/ExampleWebExceptionHandler.java | 40 +++++++++++++++++++ ...luxTestAllControllersIntegrationTests.java | 5 +++ 4 files changed, 52 insertions(+) create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleWebExceptionHandler.java diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTypeExcludeFilter.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTypeExcludeFilter.java index cf9666844c..d1650ac5cf 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTypeExcludeFilter.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTypeExcludeFilter.java @@ -32,6 +32,7 @@ import org.springframework.stereotype.Controller; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.reactive.config.WebFluxConfigurer; +import org.springframework.web.server.WebExceptionHandler; /** * {@link TypeExcludeFilter} for {@link WebFluxTest @WebFluxTest}. @@ -49,6 +50,7 @@ class WebFluxTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter { includes.add(WebFluxConfigurer.class); includes.add(Converter.class); includes.add(GenericConverter.class); + includes.add(WebExceptionHandler.class); DEFAULT_INCLUDES = Collections.unmodifiableSet(includes); } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleController1.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleController1.java index 53c31fe653..b70e319437 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleController1.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleController1.java @@ -36,4 +36,9 @@ public class ExampleController1 { return Mono.just("one"); } + @GetMapping("/one/error") + public Mono error() { + throw new RuntimeException("foo"); + } + } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleWebExceptionHandler.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleWebExceptionHandler.java new file mode 100644 index 0000000000..0840ea809b --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleWebExceptionHandler.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2018 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 + * + * http://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.test.autoconfigure.web.reactive.webclient; + +import reactor.core.publisher.Mono; + +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebExceptionHandler; + +/** + * Example {@link WebExceptionHandler} used with {@link WebFluxTest} tests. + * + * @author Madhura Bhave + */ +@Component +public class ExampleWebExceptionHandler implements WebExceptionHandler { + + @Override + public Mono handle(ServerWebExchange exchange, Throwable ex) { + exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); + return exchange.getResponse().setComplete(); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAllControllersIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAllControllersIntegrationTests.java index eb9087385f..c51b909175 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAllControllersIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAllControllersIntegrationTests.java @@ -48,4 +48,9 @@ public class WebFluxTestAllControllersIntegrationTests { .expectBody(String.class).isEqualTo("two"); } + @Test + public void webExceptionHandling() { + this.webClient.get().uri("/one/error").exchange().expectStatus().isBadRequest(); + } + }