From 15db7c51582348ebfd71b5a831dd294c2ed908e9 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 16 Oct 2025 09:15:08 +0200 Subject: [PATCH] Add nullability annotations to tests in smoke-test/spring-boot-smoke-test-actuator See gh-47263 --- .../spring-boot-smoke-test-actuator/build.gradle | 6 +++++- .../ManagementPortSampleActuatorApplicationTests.java | 6 ++++-- .../actuator/SampleActuatorApplicationTests.java | 10 ++++++++-- .../ShutdownSampleActuatorApplicationTests.java | 4 +++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/smoke-test/spring-boot-smoke-test-actuator/build.gradle b/smoke-test/spring-boot-smoke-test-actuator/build.gradle index a7a03596435..e99af8b7923 100644 --- a/smoke-test/spring-boot-smoke-test-actuator/build.gradle +++ b/smoke-test/spring-boot-smoke-test-actuator/build.gradle @@ -33,4 +33,8 @@ dependencies { testImplementation(project(":starter:spring-boot-starter-webmvc-test")) testImplementation(project(":starter:spring-boot-starter-test")) testRuntimeOnly("org.apache.httpcomponents.client5:httpclient5") -} \ No newline at end of file +} + +tasks.named("compileTestJava") { + options.nullability.checking = "tests" +} diff --git a/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortSampleActuatorApplicationTests.java b/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortSampleActuatorApplicationTests.java index d5e24c2a680..dd241cb70c7 100644 --- a/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortSampleActuatorApplicationTests.java +++ b/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ManagementPortSampleActuatorApplicationTests.java @@ -18,6 +18,7 @@ package smoketest.actuator; import java.util.Map; +import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; import smoketest.actuator.ManagementPortSampleActuatorApplicationTests.CustomErrorAttributes; @@ -96,6 +97,7 @@ class ManagementPortSampleActuatorApplicationTests { this.errorAttributes.securityContext = null; ResponseEntity> entity = asMapEntity(new TestRestTemplate("user", "password") .getForEntity("http://localhost:" + this.managementPort + "/404", Map.class)); + assertThat(this.errorAttributes.securityContext).isNotNull(); assertThat(this.errorAttributes.securityContext.getAuthentication()).isNotNull(); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(entity.getBody()).containsEntry("status", 404); @@ -108,10 +110,10 @@ class ManagementPortSampleActuatorApplicationTests { static class CustomErrorAttributes extends DefaultErrorAttributes { - private volatile SecurityContext securityContext; + private volatile @Nullable SecurityContext securityContext; @Override - public Map getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { + public Map getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { this.securityContext = SecurityContextHolder.getContext(); return super.getErrorAttributes(webRequest, options); } diff --git a/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/SampleActuatorApplicationTests.java b/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/SampleActuatorApplicationTests.java index a2ebc28a024..b830fdf540c 100644 --- a/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/SampleActuatorApplicationTests.java +++ b/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/SampleActuatorApplicationTests.java @@ -90,8 +90,10 @@ class SampleActuatorApplicationTests { ResponseEntity> entity = asMapEntity( this.restTemplate.withBasicAuth("user", "password").getForEntity("/actuator/metrics", Map.class)); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(entity.getBody()).containsKey("names"); - List names = (List) entity.getBody().get("names"); + Map body = entity.getBody(); + assertThat(body).isNotNull(); + assertThat(body).containsKey("names"); + List names = (List) body.get("names"); assertThat(names).contains("jvm.buffer.count"); } @@ -157,8 +159,11 @@ class SampleActuatorApplicationTests { this.restTemplate.withBasicAuth("user", "password").getForEntity("/actuator/configprops", Map.class)); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); Map body = entity.getBody(); + assertThat(body).isNotNull(); Map contexts = (Map) body.get("contexts"); + assertThat(contexts).isNotNull(); Map context = (Map) contexts.get(this.applicationContext.getId()); + assertThat(context).isNotNull(); Map beans = (Map) context.get("beans"); assertThat(beans).containsKey("spring.datasource-" + DataSourceProperties.class.getName()); } @@ -187,6 +192,7 @@ class SampleActuatorApplicationTests { assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(entity.getBody()).containsKey("build"); Map body = entity.getBody(); + assertThat(body).isNotNull(); Map example = (Map) body.get("example"); assertThat(example).containsEntry("someKey", "someValue"); } diff --git a/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ShutdownSampleActuatorApplicationTests.java b/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ShutdownSampleActuatorApplicationTests.java index f3bbadde50b..76849f91cbf 100644 --- a/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ShutdownSampleActuatorApplicationTests.java +++ b/smoke-test/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ShutdownSampleActuatorApplicationTests.java @@ -64,7 +64,9 @@ class ShutdownSampleActuatorApplicationTests { ResponseEntity> entity = asMapEntity(this.restTemplate.withBasicAuth("user", "password") .postForEntity("/actuator/shutdown", null, Map.class)); assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(((String) entity.getBody().get("message"))).contains("Shutting down"); + Map body = entity.getBody(); + assertThat(body).isNotNull(); + assertThat(((String) body.get("message"))).contains("Shutting down"); } @SuppressWarnings({ "unchecked", "rawtypes" })