diff --git a/module/spring-boot-servlet/build.gradle b/module/spring-boot-servlet/build.gradle index f9118dc8c8e..38948c158d9 100644 --- a/module/spring-boot-servlet/build.gradle +++ b/module/spring-boot-servlet/build.gradle @@ -47,3 +47,7 @@ dependencies { tasks.named("test") { jvmArgs += "--add-opens=java.base/java.net=ALL-UNNAMED" } + +tasks.named("compileTestJava") { + options.nullability.checking = "tests" +} diff --git a/module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/HttpEncodingAutoConfigurationTests.java b/module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/HttpEncodingAutoConfigurationTests.java index 0aec3b4258b..12bd03c2ac9 100644 --- a/module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/HttpEncodingAutoConfigurationTests.java +++ b/module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/HttpEncodingAutoConfigurationTests.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.List; import jakarta.servlet.Filter; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -45,7 +46,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; */ class HttpEncodingAutoConfigurationTests { - private AnnotationConfigServletWebApplicationContext context; + private @Nullable AnnotationConfigServletWebApplicationContext context; @AfterEach void close() { @@ -56,68 +57,71 @@ class HttpEncodingAutoConfigurationTests { @Test void defaultConfiguration() { - load(EmptyConfiguration.class); - CharacterEncodingFilter filter = this.context.getBean(CharacterEncodingFilter.class); + AnnotationConfigServletWebApplicationContext context = load(EmptyConfiguration.class); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); assertCharacterEncodingFilter(filter, "UTF-8", true, false); } @Test void disableConfiguration() { - load(EmptyConfiguration.class, "spring.servlet.encoding.enabled:false"); + AnnotationConfigServletWebApplicationContext context = load(EmptyConfiguration.class, + "spring.servlet.encoding.enabled:false"); assertThatExceptionOfType(NoSuchBeanDefinitionException.class) - .isThrownBy(() -> this.context.getBean(CharacterEncodingFilter.class)); + .isThrownBy(() -> context.getBean(CharacterEncodingFilter.class)); } @Test void customConfiguration() { - load(EmptyConfiguration.class, "spring.servlet.encoding.charset:ISO-8859-15", - "spring.servlet.encoding.force:false"); - CharacterEncodingFilter filter = this.context.getBean(CharacterEncodingFilter.class); + AnnotationConfigServletWebApplicationContext context = load(EmptyConfiguration.class, + "spring.servlet.encoding.charset:ISO-8859-15", "spring.servlet.encoding.force:false"); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); assertCharacterEncodingFilter(filter, "ISO-8859-15", false, false); } @Test void customFilterConfiguration() { - load(FilterConfiguration.class, "spring.servlet.encoding.charset:ISO-8859-15", - "spring.servlet.encoding.force:false"); - CharacterEncodingFilter filter = this.context.getBean(CharacterEncodingFilter.class); + AnnotationConfigServletWebApplicationContext context = load(FilterConfiguration.class, + "spring.servlet.encoding.charset:ISO-8859-15", "spring.servlet.encoding.force:false"); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); assertCharacterEncodingFilter(filter, "US-ASCII", false, false); } @Test void forceRequest() { - load(EmptyConfiguration.class, "spring.servlet.encoding.force-request:false"); - CharacterEncodingFilter filter = this.context.getBean(CharacterEncodingFilter.class); + AnnotationConfigServletWebApplicationContext context = load(EmptyConfiguration.class, + "spring.servlet.encoding.force-request:false"); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); assertCharacterEncodingFilter(filter, "UTF-8", false, false); } @Test void forceResponse() { - load(EmptyConfiguration.class, "spring.servlet.encoding.force-response:true"); - CharacterEncodingFilter filter = this.context.getBean(CharacterEncodingFilter.class); + AnnotationConfigServletWebApplicationContext context = load(EmptyConfiguration.class, + "spring.servlet.encoding.force-response:true"); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); assertCharacterEncodingFilter(filter, "UTF-8", true, true); } @Test void forceRequestOverridesForce() { - load(EmptyConfiguration.class, "spring.servlet.encoding.force:true", - "spring.servlet.encoding.force-request:false"); - CharacterEncodingFilter filter = this.context.getBean(CharacterEncodingFilter.class); + AnnotationConfigServletWebApplicationContext context = load(EmptyConfiguration.class, + "spring.servlet.encoding.force:true", "spring.servlet.encoding.force-request:false"); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); assertCharacterEncodingFilter(filter, "UTF-8", false, true); } @Test void forceResponseOverridesForce() { - load(EmptyConfiguration.class, "spring.servlet.encoding.force:true", - "spring.servlet.encoding.force-response:false"); - CharacterEncodingFilter filter = this.context.getBean(CharacterEncodingFilter.class); + AnnotationConfigServletWebApplicationContext context = load(EmptyConfiguration.class, + "spring.servlet.encoding.force:true", "spring.servlet.encoding.force-response:false"); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); assertCharacterEncodingFilter(filter, "UTF-8", true, false); } @Test void filterIsOrderedHighest() { - load(OrderedConfiguration.class); - List beans = new ArrayList<>(this.context.getBeansOfType(Filter.class).values()); + AnnotationConfigServletWebApplicationContext context = load(OrderedConfiguration.class); + List beans = new ArrayList<>(context.getBeansOfType(Filter.class).values()); AnnotationAwareOrderComparator.sort(beans); assertThat(beans.get(0)).isInstanceOf(CharacterEncodingFilter.class); assertThat(beans.get(1)).isInstanceOf(HiddenHttpMethodFilter.class); @@ -130,8 +134,9 @@ class HttpEncodingAutoConfigurationTests { assertThat(actual.isForceResponseEncoding()).isEqualTo(forceResponseEncoding); } - private void load(Class config, String... environment) { + private AnnotationConfigServletWebApplicationContext load(Class config, String... environment) { this.context = doLoad(new Class[] { config }, environment); + return this.context; } private AnnotationConfigServletWebApplicationContext doLoad(Class[] configs, String... environment) { diff --git a/module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/MultipartAutoConfigurationTests.java b/module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/MultipartAutoConfigurationTests.java index 016c5fe312b..5ee766ab607 100644 --- a/module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/MultipartAutoConfigurationTests.java +++ b/module/spring-boot-servlet/src/test/java/org/springframework/boot/servlet/autoconfigure/MultipartAutoConfigurationTests.java @@ -20,6 +20,7 @@ import java.net.URI; import java.util.stream.Stream; import jakarta.servlet.MultipartConfigElement; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -33,6 +34,7 @@ import org.springframework.boot.testsupport.classpath.ForkedClassPath; import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories; import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; import org.springframework.boot.tomcat.servlet.TomcatServletWebServerFactory; +import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.autoconfigure.ServerProperties; import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext; import org.springframework.context.annotation.Bean; @@ -69,7 +71,7 @@ import static org.mockito.Mockito.mock; @DirtiesUrlFactories class MultipartAutoConfigurationTests { - private AnnotationConfigServletWebServerApplicationContext context; + private @Nullable AnnotationConfigServletWebServerApplicationContext context; @AfterEach void close() { @@ -83,7 +85,7 @@ class MultipartAutoConfigurationTests { this.context = new AnnotationConfigServletWebServerApplicationContext(WebServerWithNothing.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); - verify404(); + verify404(this.context); assertThat(servlet.getMultipartResolver()).isNotNull(); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); @@ -96,7 +98,7 @@ class MultipartAutoConfigurationTests { this.context = new AnnotationConfigServletWebServerApplicationContext(configuration, BaseConfiguration.class); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)).hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); - verifyServletWorks(); + verifyServletWorks(this.context); assertThat(this.context.getBean(StandardServletMultipartResolver.class)) .isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver()); } @@ -112,7 +114,7 @@ class MultipartAutoConfigurationTests { void webServerWithAutomatedMultipartConfiguration(String server, Class configuration) { this.context = new AnnotationConfigServletWebServerApplicationContext(configuration, BaseConfiguration.class); this.context.getBean(MultipartConfigElement.class); - verifyServletWorks(); + verifyServletWorks(this.context); assertThat(this.context.getBean(StandardServletMultipartResolver.class)) .isSameAs(this.context.getBean(DispatcherServlet.class).getMultipartResolver()); } @@ -200,18 +202,22 @@ class MultipartAutoConfigurationTests { assertThat(multipartConfigElement.getMaxRequestSize()).isEqualTo(2048); } - private void verify404() throws Exception { + private void verify404(AnnotationConfigServletWebServerApplicationContext context) throws Exception { HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); + WebServer webServer = context.getWebServer(); + assertThat(webServer).isNotNull(); ClientHttpRequest request = requestFactory - .createRequest(new URI("http://localhost:" + this.context.getWebServer().getPort() + "/"), HttpMethod.GET); + .createRequest(new URI("http://localhost:" + webServer.getPort() + "/"), HttpMethod.GET); try (ClientHttpResponse response = request.execute()) { assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); } } - private void verifyServletWorks() { + private void verifyServletWorks(AnnotationConfigServletWebServerApplicationContext context) { RestTemplate restTemplate = new RestTemplate(); - String url = "http://localhost:" + this.context.getWebServer().getPort() + "/"; + WebServer webServer = context.getWebServer(); + assertThat(webServer).isNotNull(); + String url = "http://localhost:" + webServer.getPort() + "/"; assertThat(restTemplate.getForObject(url, String.class)).isEqualTo("Hello"); }