From a223834d572d4162b7efc0f27a98f3f125e02007 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 2 Aug 2023 15:28:01 +0200 Subject: [PATCH] Polish "Order auto-configured ProblemDetailsExceptionHandler beans" See gh-36288 --- .../WebFluxAutoConfigurationTests.java | 23 ++++++++----------- .../servlet/WebMvcAutoConfigurationTests.java | 19 ++++++--------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java index d6e16cfe15..b1753c4142 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java @@ -31,13 +31,13 @@ import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; -import java.util.stream.Collectors; import jakarta.validation.ValidatorFactory; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.assertj.core.api.Assertions; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -50,6 +50,8 @@ import org.springframework.boot.autoconfigure.validation.ValidationAutoConfigura import org.springframework.boot.autoconfigure.validation.ValidatorAdapter; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration.WebFluxConfig; +import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfigurationTests.OrderedControllerAdviceBeansConfiguration.HighestOrderedControllerAdvice; +import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfigurationTests.OrderedControllerAdviceBeansConfiguration.LowestOrderedControllerAdvice; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; @@ -674,21 +676,14 @@ class WebFluxAutoConfigurationTests { } @Test - void problemDetailsIsOrderedBetweenLowestAndHighestOrderedControllerHandlers() { + void problemDetailsExceptionHandlerIsOrderedAt0() { this.contextRunner.withPropertyValues("spring.webflux.problemdetails.enabled:true") .withUserConfiguration(OrderedControllerAdviceBeansConfiguration.class) - .run((context) -> { - - List> controllerAdviceClasses = ControllerAdviceBean.findAnnotatedBeans(context) - .stream() - .map(ControllerAdviceBean::getBeanType) - .collect(Collectors.toList()); - - assertThat(controllerAdviceClasses).containsExactly( - OrderedControllerAdviceBeansConfiguration.HighestOrderedControllerAdvice.class, - ProblemDetailsExceptionHandler.class, - OrderedControllerAdviceBeansConfiguration.LowestOrderedControllerAdvice.class); - }); + .run((context) -> assertThat( + ControllerAdviceBean.findAnnotatedBeans(context).stream().map(ControllerAdviceBean::getBeanType)) + .asInstanceOf(InstanceOfAssertFactories.list(Class.class)) + .containsExactly(HighestOrderedControllerAdvice.class, ProblemDetailsExceptionHandler.class, + LowestOrderedControllerAdvice.class)); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 5d89e0ec0a..1ecc2395be 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -32,7 +32,6 @@ import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; -import java.util.stream.Collectors; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -40,6 +39,7 @@ import jakarta.validation.ValidatorFactory; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.springframework.aop.support.AopUtils; @@ -979,19 +979,14 @@ class WebMvcAutoConfigurationTests { } @Test - void problemDetailsIsOrderedBetweenLowestAndHighestOrderedControllerHandlers() { + void problemDetailsExceptionHandlerIsOrderedAt0() { this.contextRunner.withPropertyValues("spring.mvc.problemdetails.enabled:true") .withUserConfiguration(OrderedControllerAdviceBeansConfiguration.class) - .run((context) -> { - - List> controllerAdviceClasses = ControllerAdviceBean.findAnnotatedBeans(context) - .stream() - .map(ControllerAdviceBean::getBeanType) - .collect(Collectors.toList()); - - assertThat(controllerAdviceClasses).containsExactly(HighestOrderedControllerAdvice.class, - ProblemDetailsExceptionHandler.class, LowestOrderedControllerAdvice.class); - }); + .run((context) -> assertThat( + ControllerAdviceBean.findAnnotatedBeans(context).stream().map(ControllerAdviceBean::getBeanType)) + .asInstanceOf(InstanceOfAssertFactories.list(Class.class)) + .containsExactly(HighestOrderedControllerAdvice.class, ProblemDetailsExceptionHandler.class, + OrderedControllerAdviceBeansConfiguration.LowestOrderedControllerAdvice.class)); } private void assertResourceHttpRequestHandler(AssertableWebApplicationContext context,