From 24b1bc4576d9786da3899d5b8c8cc1a8dbd1419b Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Tue, 19 Mar 2019 21:48:17 +0330 Subject: [PATCH 1/7] Importing the missing ErrorWebFluxAutoConfiguration. --- .../src/main/resources/META-INF/spring.factories | 3 ++- 1 file changed, 2 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 cfa9df1d2890..d05127f94215 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=\ From 023991c2b41ad269b964cb2fdb791a9790d052ba Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Tue, 19 Mar 2019 21:49:03 +0330 Subject: [PATCH 2/7] A test to make sure that the auto-configuration is imported. --- .../WebFluxTestAutoConfigurationIntegrationTests.java | 8 ++++++++ 1 file changed, 8 insertions(+) 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 7f5773404853..b33a8dfa2c42 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.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 @@ * * @author Stephane Nicoll * @author Artsiom Yudovin + * @author Ali Dehghani */ @RunWith(SpringRunner.class) @WebFluxTest @@ -75,4 +77,10 @@ public void thymeleafAutoConfigurationIsImported() { .has(importedAutoConfiguration(ThymeleafAutoConfiguration.class)); } + @Test + public void errorWebFluxAutoConfigurationIsImported() { + assertThat(this.applicationContext) + .has(importedAutoConfiguration(ErrorWebFluxAutoConfiguration.class)); + } + } From b8923027aed417a96ffc3e87799a1579ba4ddf94 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Tue, 19 Mar 2019 23:25:53 +0330 Subject: [PATCH 3/7] Added more tests. --- .../webclient/ExampleWebExceptionHandler.java | 6 +- ...luxTestAllControllersIntegrationTests.java | 3 +- ...bFluxTestWithDefaultErrorHandlerTests.java | 57 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java 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 e4eef3347fbf..3e421716d611 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 @@ -17,7 +17,9 @@ import reactor.core.publisher.Mono; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; @@ -27,9 +29,11 @@ * Example {@link WebExceptionHandler} used with {@link WebFluxTest} tests. * * @author Madhura Bhave + * @author Ali Dehghani */ @Component -public class ExampleWebExceptionHandler implements WebExceptionHandler { +@ConditionalOnProperty(name = "custom-error-handler.enable") +public class ExampleWebExceptionHandler implements ErrorWebExceptionHandler { @Override public Mono handle(ServerWebExchange exchange, Throwable ex) { 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 518f6237c33f..a8bedc7a30c1 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 @@ -29,10 +29,11 @@ * Tests for {@link WebFluxTest} when no explicit controller is defined. * * @author Stephane Nicoll + * @author Ali Dehghani */ @RunWith(SpringRunner.class) @WithMockUser -@WebFluxTest +@WebFluxTest(properties = "custom-error-handler.enable=true") public class WebFluxTestAllControllersIntegrationTests { @Autowired diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java new file mode 100644 index 000000000000..0c47e32aed30 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java @@ -0,0 +1,57 @@ +/* + * 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 org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +/** + * Tests for {@link WebFluxTest} when no explicit exception handler is defined. + * + * @author Ali Dehghani + */ +@RunWith(SpringRunner.class) +@WithMockUser +@WebFluxTest +public class WebFluxTestWithDefaultErrorHandlerTests { + + @Autowired + private WebTestClient webClient; + + @Test + public void defaultWebExceptionHandling() { + // @formatter:off + this.webClient.get().uri("/one/error").exchange() + .expectStatus().isEqualTo(INTERNAL_SERVER_ERROR) + .expectBody() + .jsonPath("$.timestamp").exists() + .jsonPath("$.status").isEqualTo(500) + .jsonPath("$.error").isEqualTo(INTERNAL_SERVER_ERROR.getReasonPhrase()) + .jsonPath("$.path").isEqualTo("/one/error") + .jsonPath("$.message").isEqualTo("foo"); + // @formatter:on + } + +} From 1c3746b21b016f22ce1c66566f5177e26f0bc35f Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Wed, 20 Mar 2019 08:11:25 +0330 Subject: [PATCH 4/7] Removing the static import. --- .../webclient/WebFluxTestWithDefaultErrorHandlerTests.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java index 0c47e32aed30..f96073d75657 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java @@ -21,12 +21,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.http.HttpStatus; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; - /** * Tests for {@link WebFluxTest} when no explicit exception handler is defined. * @@ -44,11 +43,11 @@ public class WebFluxTestWithDefaultErrorHandlerTests { public void defaultWebExceptionHandling() { // @formatter:off this.webClient.get().uri("/one/error").exchange() - .expectStatus().isEqualTo(INTERNAL_SERVER_ERROR) + .expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR) .expectBody() .jsonPath("$.timestamp").exists() .jsonPath("$.status").isEqualTo(500) - .jsonPath("$.error").isEqualTo(INTERNAL_SERVER_ERROR.getReasonPhrase()) + .jsonPath("$.error").isEqualTo("Internal Server Error") .jsonPath("$.path").isEqualTo("/one/error") .jsonPath("$.message").isEqualTo("foo"); // @formatter:on From d90d5921bba461b621a4c8aaea914bc393bee9da Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Mon, 25 Mar 2019 14:25:45 +0430 Subject: [PATCH 5/7] Registering the exception handlers with different priorities. --- .../webclient/ExampleWebExceptionHandler.java | 7 +------ ...luxTestAllControllersIntegrationTests.java | 21 ++++++++++++++++++- ...bFluxTestWithDefaultErrorHandlerTests.java | 16 ++++++++++++++ 3 files changed, 37 insertions(+), 7 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 3e421716d611..9bf7f6cd4db4 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 @@ -17,11 +17,8 @@ import reactor.core.publisher.Mono; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; -import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebExceptionHandler; @@ -31,9 +28,7 @@ * @author Madhura Bhave * @author Ali Dehghani */ -@Component -@ConditionalOnProperty(name = "custom-error-handler.enable") -public class ExampleWebExceptionHandler implements ErrorWebExceptionHandler { +public class ExampleWebExceptionHandler implements WebExceptionHandler { @Override public Mono handle(ServerWebExchange exchange, Throwable ex) { 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 a8bedc7a30c1..326a193df685 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 @@ -21,6 +21,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; @@ -33,7 +37,7 @@ */ @RunWith(SpringRunner.class) @WithMockUser -@WebFluxTest(properties = "custom-error-handler.enable=true") +@WebFluxTest public class WebFluxTestAllControllersIntegrationTests { @Autowired @@ -61,4 +65,19 @@ public void shouldFindJsonController() { this.webClient.get().uri("/json").exchange().expectStatus().isOk(); } + /** + * A test configuration to register a custom exception handler. Since the registered + * handler has the highest possible priority, the default exception handler provided + * by the Spring Boot will not get a chance to handle exceptions. + */ + @TestConfiguration + static class TestConfig { + + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + ExampleWebExceptionHandler exampleWebExceptionHandler() { + return new ExampleWebExceptionHandler(); + } + } + } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java index f96073d75657..1860925a645c 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java @@ -21,6 +21,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; import org.springframework.http.HttpStatus; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; @@ -53,4 +55,18 @@ public void defaultWebExceptionHandling() { // @formatter:on } + /** + * Registers an exception handler with the default priority, so the default handler + * provided by Spring Boot will be called first. + */ + @TestConfiguration + static class TestConfig { + + @Bean + ExampleWebExceptionHandler exampleWebExceptionHandler() { + return new ExampleWebExceptionHandler(); + } + + } + } From ae108884cf57a4fb879a18464d4bfcbac289a790 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Tue, 26 Mar 2019 08:39:40 +0430 Subject: [PATCH 6/7] Revert. --- .../webclient/ExampleWebExceptionHandler.java | 2 + ...luxTestAllControllersIntegrationTests.java | 20 ------ ...bFluxTestWithDefaultErrorHandlerTests.java | 72 ------------------- 3 files changed, 2 insertions(+), 92 deletions(-) delete mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java 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 9bf7f6cd4db4..8923a14521e5 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 @@ -19,6 +19,7 @@ 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; @@ -28,6 +29,7 @@ * @author Madhura Bhave * @author Ali Dehghani */ +@Component 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/WebFluxTestAllControllersIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAllControllersIntegrationTests.java index 326a193df685..518f6237c33f 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 @@ -21,10 +21,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; @@ -33,7 +29,6 @@ * Tests for {@link WebFluxTest} when no explicit controller is defined. * * @author Stephane Nicoll - * @author Ali Dehghani */ @RunWith(SpringRunner.class) @WithMockUser @@ -65,19 +60,4 @@ public void shouldFindJsonController() { this.webClient.get().uri("/json").exchange().expectStatus().isOk(); } - /** - * A test configuration to register a custom exception handler. Since the registered - * handler has the highest possible priority, the default exception handler provided - * by the Spring Boot will not get a chance to handle exceptions. - */ - @TestConfiguration - static class TestConfig { - - @Bean - @Order(Ordered.HIGHEST_PRECEDENCE) - ExampleWebExceptionHandler exampleWebExceptionHandler() { - return new ExampleWebExceptionHandler(); - } - } - } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java deleted file mode 100644 index 1860925a645c..000000000000 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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 org.junit.Test; -import org.junit.runner.RunWith; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.http.HttpStatus; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.reactive.server.WebTestClient; - -/** - * Tests for {@link WebFluxTest} when no explicit exception handler is defined. - * - * @author Ali Dehghani - */ -@RunWith(SpringRunner.class) -@WithMockUser -@WebFluxTest -public class WebFluxTestWithDefaultErrorHandlerTests { - - @Autowired - private WebTestClient webClient; - - @Test - public void defaultWebExceptionHandling() { - // @formatter:off - this.webClient.get().uri("/one/error").exchange() - .expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR) - .expectBody() - .jsonPath("$.timestamp").exists() - .jsonPath("$.status").isEqualTo(500) - .jsonPath("$.error").isEqualTo("Internal Server Error") - .jsonPath("$.path").isEqualTo("/one/error") - .jsonPath("$.message").isEqualTo("foo"); - // @formatter:on - } - - /** - * Registers an exception handler with the default priority, so the default handler - * provided by Spring Boot will be called first. - */ - @TestConfiguration - static class TestConfig { - - @Bean - ExampleWebExceptionHandler exampleWebExceptionHandler() { - return new ExampleWebExceptionHandler(); - } - - } - -} From 6f77b9e1935e34068089216b49dadf25a56379b5 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Tue, 26 Mar 2019 08:42:48 +0430 Subject: [PATCH 7/7] Fixed a failing test. Sine we're now importing the ErrorWebFluxAutoConfiguration, we have to register the custom exception handler with a more priority, in order to keep previous tests green. --- .../web/reactive/webclient/ExampleWebExceptionHandler.java | 3 +++ 1 file changed, 3 insertions(+) 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 8923a14521e5..fb1d09cbc884 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 @@ 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; @@ -30,6 +32,7 @@ * @author Ali Dehghani */ @Component +@Order(Ordered.HIGHEST_PRECEDENCE) public class ExampleWebExceptionHandler implements WebExceptionHandler { @Override