From 53e7484d87191a5438d94545e28063adcd98a343 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Tue, 19 Mar 2019 21:48:17 +0330 Subject: [PATCH 1/2] Add error rendering support with @WebFluxTest See gh-16266 --- .../src/main/resources/META-INF/spring.factories | 3 ++- .../reactive/webclient/ExampleWebExceptionHandler.java | 4 ++++ .../WebFluxTestAutoConfigurationIntegrationTests.java | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index 450f956d0d8..26c9971641a 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -100,7 +100,8 @@ org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\ -org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration +org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\ +org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration # AutoConfigureMockMvc auto-configuration imports org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc=\ 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 index e4eef3347fb..fb1d09cbc88 100644 --- 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 @@ -18,6 +18,8 @@ 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.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; @@ -27,8 +29,10 @@ import org.springframework.web.server.WebExceptionHandler; * Example {@link WebExceptionHandler} used with {@link WebFluxTest} tests. * * @author Madhura Bhave + * @author Ali Dehghani */ @Component +@Order(Ordered.HIGHEST_PRECEDENCE) public class ExampleWebExceptionHandler implements WebExceptionHandler { @Override diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAutoConfigurationIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAutoConfigurationIntegrationTests.java index 7f577340485..b33a8dfa2c4 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAutoConfigurationIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAutoConfigurationIntegrationTests.java @@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfigura import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration; import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; +import org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner; @@ -37,6 +38,7 @@ import static org.springframework.boot.test.autoconfigure.AutoConfigurationImpor * * @author Stephane Nicoll * @author Artsiom Yudovin + * @author Ali Dehghani */ @RunWith(SpringRunner.class) @WebFluxTest @@ -75,4 +77,10 @@ public class WebFluxTestAutoConfigurationIntegrationTests { .has(importedAutoConfiguration(ThymeleafAutoConfiguration.class)); } + @Test + public void errorWebFluxAutoConfigurationIsImported() { + assertThat(this.applicationContext) + .has(importedAutoConfiguration(ErrorWebFluxAutoConfiguration.class)); + } + } From 090f5f539710bd80995768a4a6decc8f6cb33550 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 29 Mar 2019 11:17:50 +0100 Subject: [PATCH 2/2] Polish "Add error rendering support with @WebFluxTest" Closes gh-16266 --- .../webclient/ExampleWebExceptionHandler.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) 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 index fb1d09cbc88..6df3830e13f 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -18,7 +18,7 @@ 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.core.Ordered; +import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @@ -29,16 +29,24 @@ import org.springframework.web.server.WebExceptionHandler; * Example {@link WebExceptionHandler} used with {@link WebFluxTest} tests. * * @author Madhura Bhave - * @author Ali Dehghani */ @Component -@Order(Ordered.HIGHEST_PRECEDENCE) +@Order(-2) public class ExampleWebExceptionHandler implements WebExceptionHandler { + private final ErrorWebExceptionHandler fallback; + + public ExampleWebExceptionHandler(ErrorWebExceptionHandler fallback) { + this.fallback = fallback; + } + @Override public Mono handle(ServerWebExchange exchange, Throwable ex) { - exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); - return exchange.getResponse().setComplete(); + if (ex instanceof RuntimeException && "foo".equals(ex.getMessage())) { + exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); + return exchange.getResponse().setComplete(); + } + return this.fallback.handle(exchange, ex); } }