Skip to content

Commit f8ef26c

Browse files
committed
Registering the exception handlers with different priorities.
1 parent ba29a1d commit f8ef26c

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleWebExceptionHandler.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717

1818
import reactor.core.publisher.Mono;
1919

20-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2120
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
22-
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
2321
import org.springframework.http.HttpStatus;
24-
import org.springframework.stereotype.Component;
2522
import org.springframework.web.server.ServerWebExchange;
2623
import org.springframework.web.server.WebExceptionHandler;
2724

@@ -31,9 +28,7 @@
3128
* @author Madhura Bhave
3229
* @author Ali Dehghani
3330
*/
34-
@Component
35-
@ConditionalOnProperty(name = "custom-error-handler.enable")
36-
public class ExampleWebExceptionHandler implements ErrorWebExceptionHandler {
31+
public class ExampleWebExceptionHandler implements WebExceptionHandler {
3732

3833
@Override
3934
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestAllControllersIntegrationTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
24+
import org.springframework.boot.test.context.TestConfiguration;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.core.Ordered;
27+
import org.springframework.core.annotation.Order;
2428
import org.springframework.security.test.context.support.WithMockUser;
2529
import org.springframework.test.context.junit4.SpringRunner;
2630
import org.springframework.test.web.reactive.server.WebTestClient;
@@ -33,7 +37,7 @@
3337
*/
3438
@RunWith(SpringRunner.class)
3539
@WithMockUser
36-
@WebFluxTest(properties = "custom-error-handler.enable=true")
40+
@WebFluxTest
3741
public class WebFluxTestAllControllersIntegrationTests {
3842

3943
@Autowired
@@ -61,4 +65,19 @@ public void shouldFindJsonController() {
6165
this.webClient.get().uri("/json").exchange().expectStatus().isOk();
6266
}
6367

68+
/**
69+
* A test configuration to register a custom exception handler. Since the registered
70+
* handler has the highest possible priority, the default exception handler provided
71+
* by the Spring Boot will not get a chance to handle exceptions.
72+
*/
73+
@TestConfiguration
74+
static class TestConfig {
75+
76+
@Bean
77+
@Order(Ordered.HIGHEST_PRECEDENCE)
78+
ExampleWebExceptionHandler exampleWebExceptionHandler() {
79+
return new ExampleWebExceptionHandler();
80+
}
81+
}
82+
6483
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestWithDefaultErrorHandlerTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
24+
import org.springframework.boot.test.context.TestConfiguration;
25+
import org.springframework.context.annotation.Bean;
2426
import org.springframework.http.HttpStatus;
2527
import org.springframework.security.test.context.support.WithMockUser;
2628
import org.springframework.test.context.junit4.SpringRunner;
@@ -53,4 +55,18 @@ public void defaultWebExceptionHandling() {
5355
// @formatter:on
5456
}
5557

58+
/**
59+
* Registers an exception handler with the default priority, so the default handler
60+
* provided by Spring Boot will be called first.
61+
*/
62+
@TestConfiguration
63+
static class TestConfig {
64+
65+
@Bean
66+
ExampleWebExceptionHandler exampleWebExceptionHandler() {
67+
return new ExampleWebExceptionHandler();
68+
}
69+
70+
}
71+
5672
}

0 commit comments

Comments
 (0)