diff --git a/module/spring-boot-webflux/build.gradle b/module/spring-boot-webflux/build.gradle index 983439670f8..fcc66e43844 100644 --- a/module/spring-boot-webflux/build.gradle +++ b/module/spring-boot-webflux/build.gradle @@ -58,3 +58,7 @@ dependencies { testRuntimeOnly("ch.qos.logback:logback-classic") testRuntimeOnly("jakarta.servlet:jakarta.servlet-api") } + +tasks.named("compileTestJava") { + options.nullability.checking = "tests" +} diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/actuate/endpoint/web/ControllerEndpointHandlerMappingTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/actuate/endpoint/web/ControllerEndpointHandlerMappingTests.java index 7f2f01c1679..fd889592467 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/actuate/endpoint/web/ControllerEndpointHandlerMappingTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/actuate/endpoint/web/ControllerEndpointHandlerMappingTests.java @@ -16,9 +16,11 @@ package org.springframework.boot.webflux.actuate.endpoint.web; +import java.lang.reflect.Method; import java.time.Duration; import java.util.Arrays; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.endpoint.Access; @@ -96,7 +98,8 @@ class ControllerEndpointHandlerMappingTests { .isThrownBy(() -> getHandler(mapping, HttpMethod.POST, "/actuator/first")); } - private Object getHandler(ControllerEndpointHandlerMapping mapping, HttpMethod method, String requestURI) { + private @Nullable Object getHandler(ControllerEndpointHandlerMapping mapping, HttpMethod method, + String requestURI) { return mapping.getHandler(exchange(method, requestURI)).block(Duration.ofSeconds(30)); } @@ -109,7 +112,9 @@ class ControllerEndpointHandlerMappingTests { } private HandlerMethod handlerOf(Object source, String methodName) { - return new HandlerMethod(source, ReflectionUtils.findMethod(source.getClass(), methodName)); + Method method = ReflectionUtils.findMethod(source.getClass(), methodName); + assertThat(method).isNotNull(); + return new HandlerMethod(source, method); } private MockServerWebExchange exchange(HttpMethod method, String requestURI) { diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/HttpHandlerAutoConfigurationTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/HttpHandlerAutoConfigurationTests.java index f509992962f..e2e4cb7d70e 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/HttpHandlerAutoConfigurationTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/HttpHandlerAutoConfigurationTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.webflux.autoconfigure; import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; @@ -104,12 +105,12 @@ class HttpHandlerAutoConfigurationTests { @Bean HttpHandler customHttpHandler() { - return (serverHttpRequest, serverHttpResponse) -> null; + return (serverHttpRequest, serverHttpResponse) -> Mono.empty(); } @Bean RouterFunction routerFunction() { - return route(GET("/test"), (serverRequest) -> null); + return route(GET("/test"), (serverRequest) -> Mono.empty()); } } diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java index a8c21f69086..434a9fa8f6f 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfigurationTests.java @@ -38,11 +38,13 @@ 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.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.JRE; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import reactor.core.publisher.Mono; import org.springframework.aop.support.AopUtils; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -109,6 +111,7 @@ import org.springframework.web.reactive.resource.CachingResourceTransformer; import org.springframework.web.reactive.resource.PathResourceResolver; import org.springframework.web.reactive.resource.ResourceWebHandler; import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver; +import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer; import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler; @@ -178,8 +181,10 @@ class WebFluxAutoConfigurationTests { void shouldRegisterCustomHandlerMethodArgumentResolver() { this.contextRunner.withUserConfiguration(CustomArgumentResolvers.class).run((context) -> { RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class); + ArgumentResolverConfigurer configurer = adapter.getArgumentResolverConfigurer(); + assertThat(configurer).isNotNull(); List customResolvers = (List) ReflectionTestUtils - .getField(adapter.getArgumentResolverConfigurer(), "customResolvers"); + .getField(configurer, "customResolvers"); assertThat(customResolvers).contains(context.getBean("firstResolver", HandlerMethodArgumentResolver.class), context.getBean("secondResolver", HandlerMethodArgumentResolver.class)); }); @@ -564,9 +569,9 @@ class WebFluxAutoConfigurationTests { void whenFixedLocalContextResolverIsUsedThenAcceptLanguagesHeaderIsIgnored() { this.contextRunner.withPropertyValues("spring.web.locale:en_UK", "spring.web.locale-resolver=fixed") .run((context) -> { - MockServerHttpRequest request = MockServerHttpRequest.get("/") - .acceptLanguageAsLocales(StringUtils.parseLocaleString("nl_NL")) - .build(); + Locale locale = StringUtils.parseLocaleString("nl_NL"); + assertThat(locale).isNotNull(); + MockServerHttpRequest request = MockServerHttpRequest.get("/").acceptLanguageAsLocales(locale).build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); LocaleContextResolver localeContextResolver = context.getBean(LocaleContextResolver.class); assertThat(localeContextResolver).isInstanceOf(FixedLocaleContextResolver.class); @@ -578,14 +583,14 @@ class WebFluxAutoConfigurationTests { @Test void whenAcceptHeaderLocaleContextResolverIsUsedThenAcceptLanguagesHeaderIsHonoured() { this.contextRunner.withPropertyValues("spring.web.locale:en_UK").run((context) -> { - MockServerHttpRequest request = MockServerHttpRequest.get("/") - .acceptLanguageAsLocales(StringUtils.parseLocaleString("nl_NL")) - .build(); + Locale locale = StringUtils.parseLocaleString("nl_NL"); + assertThat(locale).isNotNull(); + MockServerHttpRequest request = MockServerHttpRequest.get("/").acceptLanguageAsLocales(locale).build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); LocaleContextResolver localeContextResolver = context.getBean(LocaleContextResolver.class); assertThat(localeContextResolver).isInstanceOf(AcceptHeaderLocaleContextResolver.class); LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange); - assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("nl_NL")); + assertThat(localeContext.getLocale()).isEqualTo(locale); }); } @@ -674,12 +679,15 @@ class WebFluxAutoConfigurationTests { .run(assertExchangeWithSession((exchange) -> { List cookies = exchange.getResponse().getCookies().get("JSESSIONID"); assertThat(cookies).isNotEmpty(); - assertThat(cookies).allMatch((cookie) -> cookie.getDomain().equals(".example.com")); - assertThat(cookies).allMatch((cookie) -> cookie.getPath().equals("/example")); + assertThat(cookies) + .allMatch((cookie) -> cookie.getDomain() != null && cookie.getDomain().equals(".example.com")); + assertThat(cookies) + .allMatch((cookie) -> cookie.getPath() != null && cookie.getPath().equals("/example")); assertThat(cookies).allMatch((cookie) -> cookie.getMaxAge().equals(Duration.ofSeconds(60))); assertThat(cookies).allMatch((cookie) -> !cookie.isHttpOnly()); assertThat(cookies).allMatch((cookie) -> !cookie.isSecure()); - assertThat(cookies).allMatch((cookie) -> cookie.getSameSite().equals("Strict")); + assertThat(cookies) + .allMatch((cookie) -> cookie.getSameSite() != null && cookie.getSameSite().equals("Strict")); assertThat(cookies).allMatch(ResponseCookie::isPartitioned); })); } @@ -906,6 +914,7 @@ class WebFluxAutoConfigurationTests { MockServerWebExchange webExchange = MockServerWebExchange.from(request); WebSessionManager webSessionManager = context.getBean(WebSessionManager.class); WebSession webSession = webSessionManager.getSession(webExchange).block(); + assertThat(webSession).isNotNull(); webSession.start(); webExchange.getResponse().setComplete().block(); exchange.accept(webExchange); @@ -1024,7 +1033,7 @@ class WebFluxAutoConfigurationTests { @Bean HttpHandler httpHandler() { - return (serverHttpRequest, serverHttpResponse) -> null; + return (serverHttpRequest, serverHttpResponse) -> Mono.empty(); } } @@ -1198,7 +1207,7 @@ class WebFluxAutoConfigurationTests { } @Override - public void setLocaleContext(ServerWebExchange exchange, LocaleContext localeContext) { + public void setLocaleContext(ServerWebExchange exchange, @Nullable LocaleContext localeContext) { } } diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WelcomePageRouterFunctionFactoryTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WelcomePageRouterFunctionFactoryTests.java index a19ecc1eb96..b991d1d66ab 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WelcomePageRouterFunctionFactoryTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/WelcomePageRouterFunctionFactoryTests.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.Locale; import java.util.Map; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import reactor.core.publisher.Mono; @@ -36,6 +37,8 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.web.reactive.function.server.HandlerStrategies; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.result.view.View; import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.server.ServerWebExchange; @@ -166,13 +169,17 @@ class WelcomePageRouterFunctionFactoryTests { private WebTestClient withStaticIndex() { WelcomePageRouterFunctionFactory factory = factoryWithoutTemplateSupport(this.indexLocations, "/**"); - return WebTestClient.bindToRouterFunction(factory.createRouterFunction()).build(); + RouterFunction routerFunction = factory.createRouterFunction(); + assertThat(routerFunction).isNotNull(); + return WebTestClient.bindToRouterFunction(routerFunction).build(); } private WebTestClient withTemplateIndex() { WelcomePageRouterFunctionFactory factory = factoryWithTemplateSupport(this.noIndexLocations); TestViewResolver testViewResolver = new TestViewResolver(); - return WebTestClient.bindToRouterFunction(factory.createRouterFunction()) + RouterFunction routerFunction = factory.createRouterFunction(); + assertThat(routerFunction).isNotNull(); + return WebTestClient.bindToRouterFunction(routerFunction) .handlerStrategies(HandlerStrategies.builder().viewResolver(testViewResolver).build()) .build(); } @@ -180,7 +187,9 @@ class WelcomePageRouterFunctionFactoryTests { private WebTestClient withStaticAndTemplateIndex() { WelcomePageRouterFunctionFactory factory = factoryWithTemplateSupport(this.indexLocations); TestViewResolver testViewResolver = new TestViewResolver(); - return WebTestClient.bindToRouterFunction(factory.createRouterFunction()) + RouterFunction routerFunction = factory.createRouterFunction(); + assertThat(routerFunction).isNotNull(); + return WebTestClient.bindToRouterFunction(routerFunction) .handlerStrategies(HandlerStrategies.builder().viewResolver(testViewResolver).build()) .build(); } @@ -226,7 +235,8 @@ class WelcomePageRouterFunctionFactoryTests { private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); @Override - public Mono render(Map model, MediaType contentType, ServerWebExchange exchange) { + public Mono render(@Nullable Map model, @Nullable MediaType contentType, + ServerWebExchange exchange) { DataBuffer buffer = this.bufferFactory.wrap("welcome-page-template".getBytes(StandardCharsets.UTF_8)); return exchange.getResponse().writeWith(Mono.just(buffer)); } diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/ControllerEndpointWebFluxIntegrationTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/ControllerEndpointWebFluxIntegrationTests.java index 238643e84c7..57fd7e6a7c7 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/ControllerEndpointWebFluxIntegrationTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/ControllerEndpointWebFluxIntegrationTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.webflux.autoconfigure.actuate.web; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -44,11 +45,13 @@ import org.springframework.web.bind.annotation.GetMapping; @SuppressWarnings("removal") class ControllerEndpointWebFluxIntegrationTests { - private AnnotationConfigReactiveWebApplicationContext context; + private @Nullable AnnotationConfigReactiveWebApplicationContext context; @AfterEach void close() { - this.context.close(); + if (this.context != null) { + this.context.close(); + } } @Test diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointAccessIntegrationTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointAccessIntegrationTests.java index ad4ac56fea5..dce47d3809e 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointAccessIntegrationTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxEndpointAccessIntegrationTests.java @@ -30,6 +30,7 @@ import org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration; import org.springframework.boot.reactor.netty.autoconfigure.NettyReactiveWebServerAutoConfiguration; import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; +import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; import org.springframework.boot.web.server.reactive.context.ReactiveWebServerApplicationContext; import org.springframework.boot.webflux.autoconfigure.HttpHandlerAutoConfiguration; @@ -137,9 +138,10 @@ class WebFluxEndpointAccessIntegrationTests { } private WebTestClient createClient(AssertableReactiveWebApplicationContext context) { - int port = context.getSourceApplicationContext(ReactiveWebServerApplicationContext.class) - .getWebServer() - .getPort(); + WebServer webServer = context.getSourceApplicationContext(ReactiveWebServerApplicationContext.class) + .getWebServer(); + assertThat(webServer).isNotNull(); + int port = webServer.getPort(); ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder() .codecs((configurer) -> configurer.defaultCodecs().maxInMemorySize(-1)) .build(); diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxManagementChildContextConfigurationIntegrationTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxManagementChildContextConfigurationIntegrationTests.java index 34c21ae50b3..ade1f3cb0f5 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxManagementChildContextConfigurationIntegrationTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/actuate/web/WebFluxManagementChildContextConfigurationIntegrationTests.java @@ -26,6 +26,7 @@ import java.util.function.Consumer; import org.apache.catalina.Valve; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.valves.AccessLogValve; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -80,6 +81,7 @@ class WebFluxManagementChildContextConfigurationIntegrationTests { .withPropertyValues("server.port=0", "management.server.port=0", "management.endpoints.web.exposure.include=*"); @TempDir + @SuppressWarnings("NullAway.Init") Path temp; @Test @@ -122,7 +124,7 @@ class WebFluxManagementChildContextConfigurationIntegrationTests { }); } - private AccessLogValve findAccessLogValve() { + private @Nullable AccessLogValve findAccessLogValve() { assertThat(this.webServers).hasSize(2); Tomcat tomcat = ((TomcatWebServer) this.webServers.get(1)).getTomcat(); for (Valve valve : tomcat.getEngine().getPipeline().getValves()) { diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/error/DefaultErrorWebExceptionHandlerIntegrationTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/error/DefaultErrorWebExceptionHandlerIntegrationTests.java index 599d671b4ed..d19a3cc4c4b 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/error/DefaultErrorWebExceptionHandlerIntegrationTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/error/DefaultErrorWebExceptionHandlerIntegrationTests.java @@ -22,6 +22,7 @@ import java.util.LinkedHashMap; import java.util.Map; import jakarta.validation.Valid; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -568,7 +569,7 @@ class DefaultErrorWebExceptionHandlerIntegrationTests { private static final class LogIdFilter implements WebFilter { - private String logId; + private @Nullable String logId; @Override public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { @@ -576,7 +577,7 @@ class DefaultErrorWebExceptionHandlerIntegrationTests { return chain.filter(exchange); } - String getLogId() { + @Nullable String getLogId() { return this.logId; } @@ -627,8 +628,9 @@ class DefaultErrorWebExceptionHandlerIntegrationTests { ErrorAttributes errorAttributes() { return new DefaultErrorAttributes() { @Override - public Map getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { - Map errorAttributes = super.getErrorAttributes(request, options); + public Map getErrorAttributes(ServerRequest request, + ErrorAttributeOptions options) { + Map errorAttributes = super.getErrorAttributes(request, options); errorAttributes.put("error", "custom error"); errorAttributes.put("newAttribute", "value"); errorAttributes.remove("path"); @@ -648,8 +650,9 @@ class DefaultErrorWebExceptionHandlerIntegrationTests { return new DefaultErrorAttributes() { @Override - public Map getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { - Map errorAttributes = new HashMap<>(); + public Map getErrorAttributes(ServerRequest request, + ErrorAttributeOptions options) { + Map errorAttributes = new HashMap<>(); errorAttributes.put("status", 400); errorAttributes.put("error", "custom error"); return errorAttributes; @@ -693,8 +696,10 @@ class DefaultErrorWebExceptionHandlerIntegrationTests { return new DefaultErrorAttributes() { @Override - public Map getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { - Map attributes = new LinkedHashMap<>(super.getErrorAttributes(request, options)); + public Map getErrorAttributes(ServerRequest request, + ErrorAttributeOptions options) { + Map attributes = new LinkedHashMap<>( + super.getErrorAttributes(request, options)); attributes.remove("status"); return attributes; } diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/error/DummyBody.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/error/DummyBody.java index d65f8209486..a437175e595 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/error/DummyBody.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/error/DummyBody.java @@ -21,6 +21,7 @@ import jakarta.validation.constraints.NotNull; public class DummyBody { @NotNull + @SuppressWarnings("NullAway.Init") private String content; public String getContent() { diff --git a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/error/DefaultErrorAttributesTests.java b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/error/DefaultErrorAttributesTests.java index 1af68bebed1..68e7ea9d988 100644 --- a/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/error/DefaultErrorAttributesTests.java +++ b/module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/error/DefaultErrorAttributesTests.java @@ -22,6 +22,7 @@ import java.util.Date; import java.util.List; import java.util.Map; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; import org.springframework.boot.web.error.ErrorAttributeOptions; @@ -77,8 +78,8 @@ class DefaultErrorAttributesTests { @Test void includeTimeStamp() { MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND), - ErrorAttributeOptions.defaults()); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, NOT_FOUND), ErrorAttributeOptions.defaults()); assertThat(attributes.get("timestamp")).isInstanceOf(Date.class); } @@ -86,8 +87,8 @@ class DefaultErrorAttributesTests { void defaultStatusCode() { Error error = new OutOfMemoryError("Test error"); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error), - ErrorAttributeOptions.defaults()); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, error), ErrorAttributeOptions.defaults()); assertThat(attributes).containsEntry("error", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); assertThat(attributes).containsEntry("status", 500); } @@ -96,8 +97,8 @@ class DefaultErrorAttributesTests { void annotatedResponseStatusCode() { Exception error = new CustomException(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error), - ErrorAttributeOptions.defaults()); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, error), ErrorAttributeOptions.defaults()); assertThat(attributes).containsEntry("error", HttpStatus.EXPECTATION_FAILED.getReasonPhrase()); assertThat(attributes).doesNotContainKey("message"); assertThat(attributes).containsEntry("status", HttpStatus.EXPECTATION_FAILED.value()); @@ -107,7 +108,8 @@ class DefaultErrorAttributesTests { void annotatedResponseStatusCodeWithExceptionMessage() { Exception error = new CustomException("Test Message"); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error), + Map attributes = this.errorAttributes.getErrorAttributes( + buildServerRequest(request, error), ErrorAttributeOptions.of(Include.MESSAGE, Include.STATUS, Include.ERROR)); assertThat(attributes).containsEntry("error", HttpStatus.EXPECTATION_FAILED.getReasonPhrase()); assertThat(attributes).containsEntry("message", "Test Message"); @@ -118,7 +120,8 @@ class DefaultErrorAttributesTests { void annotatedResponseStatusCodeWithCustomReasonPhrase() { Exception error = new Custom2Exception(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error), + Map attributes = this.errorAttributes.getErrorAttributes( + buildServerRequest(request, error), ErrorAttributeOptions.of(Include.MESSAGE, Include.STATUS, Include.ERROR)); assertThat(attributes).containsEntry("error", HttpStatus.EXPECTATION_FAILED.getReasonPhrase()); assertThat(attributes).containsEntry("status", HttpStatus.EXPECTATION_FAILED.value()); @@ -128,8 +131,8 @@ class DefaultErrorAttributesTests { @Test void includeStatusCode() { MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND), - ErrorAttributeOptions.defaults()); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, NOT_FOUND), ErrorAttributeOptions.defaults()); assertThat(attributes).containsEntry("error", HttpStatus.NOT_FOUND.getReasonPhrase()); assertThat(attributes).containsEntry("status", 404); } @@ -139,7 +142,7 @@ class DefaultErrorAttributesTests { Error error = new OutOfMemoryError("Test error"); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); ServerRequest serverRequest = buildServerRequest(request, error); - Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.of(Include.MESSAGE)); assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error); assertThat(attributes).doesNotContainKey("exception"); @@ -151,7 +154,7 @@ class DefaultErrorAttributesTests { Error error = new OutOfMemoryError("Test error"); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); ServerRequest serverRequest = buildServerRequest(request, error); - Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.defaults()); assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error); assertThat(attributes).doesNotContainKey("message"); @@ -163,7 +166,7 @@ class DefaultErrorAttributesTests { this.errorAttributes = new DefaultErrorAttributes(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); ServerRequest serverRequest = buildServerRequest(request, error); - Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.of(Include.EXCEPTION, Include.MESSAGE)); assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error); assertThat(attributes).containsEntry("exception", RuntimeException.class.getName()); @@ -177,7 +180,7 @@ class DefaultErrorAttributesTests { this.errorAttributes = new DefaultErrorAttributes(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); ServerRequest serverRequest = buildServerRequest(request, error); - Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.of(Include.EXCEPTION, Include.MESSAGE, Include.STATUS)); assertThat(attributes).containsEntry("status", 400); assertThat(attributes).containsEntry("message", "invalid request"); @@ -192,7 +195,7 @@ class DefaultErrorAttributesTests { this.errorAttributes = new DefaultErrorAttributes(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); ServerRequest serverRequest = buildServerRequest(request, error); - Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.of(Include.EXCEPTION, Include.MESSAGE, Include.STATUS)); assertThat(attributes).containsEntry("status", 406); assertThat(attributes).containsEntry("message", "could not process request"); @@ -204,8 +207,8 @@ class DefaultErrorAttributesTests { void notIncludeTrace() { RuntimeException ex = new RuntimeException("Test"); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), - ErrorAttributeOptions.defaults()); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, ex), ErrorAttributeOptions.defaults()); assertThat(attributes).doesNotContainKey("trace"); } @@ -213,40 +216,42 @@ class DefaultErrorAttributesTests { void includeTrace() { RuntimeException ex = new RuntimeException("Test"); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), - ErrorAttributeOptions.of(Include.STACK_TRACE)); - assertThat(attributes.get("trace").toString()).startsWith("java.lang"); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, ex), ErrorAttributeOptions.of(Include.STACK_TRACE)); + Object trace = attributes.get("trace"); + assertThat(trace).isNotNull(); + assertThat(trace.toString()).startsWith("java.lang"); } @Test void includePathByDefault() { MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND), - ErrorAttributeOptions.defaults()); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, NOT_FOUND), ErrorAttributeOptions.defaults()); assertThat(attributes).containsEntry("path", "/test"); } @Test void includePath() { MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND), - ErrorAttributeOptions.of(Include.PATH)); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, NOT_FOUND), ErrorAttributeOptions.of(Include.PATH)); assertThat(attributes).containsEntry("path", "/test"); } @Test void pathShouldIncludeContext() { MockServerHttpRequest request = MockServerHttpRequest.get("/context/test").contextPath("/context").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND), - ErrorAttributeOptions.of(Include.PATH)); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, NOT_FOUND), ErrorAttributeOptions.of(Include.PATH)); assertThat(attributes).containsEntry("path", "/context/test"); } @Test void excludePath() { MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND), - ErrorAttributeOptions.of()); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, NOT_FOUND), ErrorAttributeOptions.of()); assertThat(attributes).doesNotContainEntry("path", "/test"); } @@ -254,7 +259,7 @@ class DefaultErrorAttributesTests { void includeLogPrefix() { MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); ServerRequest serverRequest = buildServerRequest(request, NOT_FOUND); - Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.defaults()); assertThat(attributes).containsEntry("requestId", serverRequest.exchange().getRequest().getId()); } @@ -267,8 +272,8 @@ class DefaultErrorAttributesTests { bindingResult.addError(new ObjectError("c", "d")); Exception ex = new WebExchangeBindException(stringParam, bindingResult); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), - ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS)); + Map attributes = this.errorAttributes.getErrorAttributes( + buildServerRequest(request, ex), ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS)); assertThat(attributes.get("message")).asString() .startsWith("Validation failed for argument at index 0 in method: " + "int " + getClass().getName() + ".method(java.lang.String), with 1 error(s)"); @@ -284,7 +289,7 @@ class DefaultErrorAttributesTests { bindingResult.addError(new ObjectError("c", "d")); Exception ex = new WebExchangeBindException(stringParam, bindingResult); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes( + Map attributes = this.errorAttributes.getErrorAttributes( buildServerRequest(request, new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid", ex)), ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS)); assertThat(attributes.get("message")).isEqualTo("Invalid"); @@ -305,8 +310,8 @@ class DefaultErrorAttributesTests { }))); HandlerMethodValidationException ex = new HandlerMethodValidationException(methodValidationResult); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), - ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS)); + Map attributes = this.errorAttributes.getErrorAttributes( + buildServerRequest(request, ex), ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS)); assertThat(attributes.get("message")).asString() .isEqualTo( "Validation failed for method='public java.lang.String java.lang.String.substring(int)'. Error count: 1"); @@ -322,8 +327,8 @@ class DefaultErrorAttributesTests { bindingResult.addError(new ObjectError("c", "d")); Exception ex = new WebExchangeBindException(stringParam, bindingResult); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), - ErrorAttributeOptions.defaults()); + Map attributes = this.errorAttributes + .getErrorAttributes(buildServerRequest(request, ex), ErrorAttributeOptions.defaults()); assertThat(attributes).doesNotContainKey("message"); assertThat(attributes).doesNotContainKey("errors"); } @@ -342,8 +347,8 @@ class DefaultErrorAttributesTests { List.of(parameterValidationResult)); HandlerMethodValidationException ex = new HandlerMethodValidationException(methodValidationResult); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); - Map attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), - ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS)); + Map attributes = this.errorAttributes.getErrorAttributes( + buildServerRequest(request, ex), ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS)); assertThat(attributes.get("message")).asString() .isEqualTo( "Validation failed for method='public java.lang.String java.lang.String.substring(int)'. Error count: 1"); @@ -358,7 +363,7 @@ class DefaultErrorAttributesTests { this.errorAttributes = new DefaultErrorAttributes(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); ServerRequest serverRequest = buildServerRequest(request, error); - Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.defaults().excluding(Include.STATUS)); assertThat(attributes).doesNotContainKey("status"); } @@ -370,7 +375,7 @@ class DefaultErrorAttributesTests { this.errorAttributes = new DefaultErrorAttributes(); MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); ServerRequest serverRequest = buildServerRequest(request, error); - Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, + Map attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.defaults().excluding(Include.ERROR)); assertThat(attributes).doesNotContainKey("error"); }