diff --git a/module/spring-boot-restclient/build.gradle b/module/spring-boot-restclient/build.gradle index b156e4ad5b7..033bb26e76f 100644 --- a/module/spring-boot-restclient/build.gradle +++ b/module/spring-boot-restclient/build.gradle @@ -45,3 +45,7 @@ dependencies { testRuntimeOnly("jakarta.servlet:jakarta.servlet-api") testRuntimeOnly("org.springframework:spring-webflux") } + +tasks.named("compileTestJava") { + options.nullability.checking = "tests" +} diff --git a/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RestTemplateBuilderTests.java b/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RestTemplateBuilderTests.java index 53c4a532bbf..34ba6f5037e 100644 --- a/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RestTemplateBuilderTests.java +++ b/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RestTemplateBuilderTests.java @@ -81,12 +81,15 @@ class RestTemplateBuilderTests { private final RestTemplateBuilder builder = new RestTemplateBuilder(); @Mock + @SuppressWarnings("NullAway.Init") private HttpMessageConverter messageConverter; @Mock + @SuppressWarnings("NullAway.Init") private ClientHttpRequestInterceptor interceptor; @Test + @SuppressWarnings("NullAway") // Test null check void createWhenCustomizersAreNullShouldThrowException() { RestTemplateCustomizer[] customizers = null; assertThatIllegalArgumentException().isThrownBy(() -> new RestTemplateBuilder(customizers)) @@ -134,6 +137,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void messageConvertersWhenConvertersAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.messageConverters((HttpMessageConverter[]) null)) @@ -141,6 +145,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void messageConvertersCollectionWhenConvertersAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.messageConverters((Set>) null)) @@ -162,6 +167,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void additionalMessageConvertersWhenConvertersAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.additionalMessageConverters((HttpMessageConverter[]) null)) @@ -169,6 +175,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void additionalMessageConvertersCollectionWhenConvertersAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.additionalMessageConverters((Set>) null)) @@ -199,6 +206,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void interceptorsWhenInterceptorsAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.interceptors((ClientHttpRequestInterceptor[]) null)) @@ -206,6 +214,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void interceptorsCollectionWhenInterceptorsAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.interceptors((Set) null)) @@ -227,6 +236,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void additionalInterceptorsWhenInterceptorsAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.additionalInterceptors((ClientHttpRequestInterceptor[]) null)) @@ -234,6 +244,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void additionalInterceptorsCollectionWhenInterceptorsAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.additionalInterceptors((Set) null)) @@ -248,6 +259,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void requestFactoryClassWhenFactoryIsNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.requestFactory((Class) null)) @@ -267,6 +279,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void requestFactoryWhenSupplierIsNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.requestFactory((Supplier) null)) @@ -281,6 +294,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void uriTemplateHandlerWhenHandlerIsNullShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> this.builder.uriTemplateHandler(null)) .withMessageContaining("'uriTemplateHandler' must not be null"); @@ -294,6 +308,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void errorHandlerWhenHandlerIsNullShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> this.builder.errorHandler(null)) .withMessageContaining("'errorHandler' must not be null"); @@ -366,12 +381,14 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void customizersWhenCustomizersAreNullShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> this.builder.customizers((RestTemplateCustomizer[]) null)) .withMessageContaining("'customizers' must not be null"); } @Test + @SuppressWarnings("NullAway") // Test null check void customizersCollectionWhenCustomizersAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.customizers((Set) null)) @@ -405,6 +422,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void additionalCustomizersWhenCustomizersAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.additionalCustomizers((RestTemplateCustomizer[]) null)) @@ -412,6 +430,7 @@ class RestTemplateBuilderTests { } @Test + @SuppressWarnings("NullAway") // Test null check void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> this.builder.additionalCustomizers((Set) null)) @@ -486,8 +505,10 @@ class RestTemplateBuilderTests { } private ClientHttpRequest createRequest(RestTemplate template) { - return ReflectionTestUtils.invokeMethod(template, "createRequest", URI.create("http://localhost"), - HttpMethod.GET); + ClientHttpRequest request = ReflectionTestUtils.invokeMethod(template, "createRequest", + URI.create("http://localhost"), HttpMethod.GET); + assertThat(request).isNotNull(); + return request; } static class RestTemplateSubclass extends RestTemplate { diff --git a/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RootUriTemplateHandlerTests.java b/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RootUriTemplateHandlerTests.java index ffc7b99c80d..9140f2e8c82 100644 --- a/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RootUriTemplateHandlerTests.java +++ b/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RootUriTemplateHandlerTests.java @@ -48,6 +48,7 @@ class RootUriTemplateHandlerTests { private URI uri; @Mock + @SuppressWarnings("NullAway.Init") public UriTemplateHandler delegate; public UriTemplateHandler handler; @@ -59,6 +60,7 @@ class RootUriTemplateHandlerTests { } @Test + @SuppressWarnings("NullAway") // Test null check void createWithNullRootUriShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy(() -> new RootUriTemplateHandler((String) null, mock(UriTemplateHandler.class))) @@ -66,6 +68,7 @@ class RootUriTemplateHandlerTests { } @Test + @SuppressWarnings("NullAway") // Test null check void createWithNullHandlerShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> new RootUriTemplateHandler("https://example.com", null)) .withMessageContaining("'handler' must not be null"); diff --git a/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/AutoConfiguredRestClientSslTests.java b/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/AutoConfiguredRestClientSslTests.java index 79be8b78633..98c1ede6136 100644 --- a/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/AutoConfiguredRestClientSslTests.java +++ b/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/AutoConfiguredRestClientSslTests.java @@ -52,12 +52,15 @@ class AutoConfiguredRestClientSslTests { .withConnectTimeout(Duration.ofSeconds(30)); @Mock + @SuppressWarnings("NullAway.Init") private SslBundles sslBundles; @Mock + @SuppressWarnings("NullAway.Init") private ClientHttpRequestFactoryBuilder factoryBuilder; @Mock + @SuppressWarnings("NullAway.Init") private ClientHttpRequestFactory factory; private AutoConfiguredRestClientSsl restClientSsl; diff --git a/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/service/HttpServiceClientAutoConfigurationTests.java b/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/service/HttpServiceClientAutoConfigurationTests.java index 8f3d8c0786c..c2556f8709c 100644 --- a/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/service/HttpServiceClientAutoConfigurationTests.java +++ b/module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/service/HttpServiceClientAutoConfigurationTests.java @@ -222,7 +222,9 @@ class HttpServiceClientAutoConfigurationTests { } MockRestServiceServer getMock(String name) { - return this.mocks.get(name); + MockRestServiceServer mock = this.mocks.get(name); + assertThat(mock).isNotNull(); + return mock; } }