Add nullability annotations to tests in module/spring-boot-restclient

See gh-47263
This commit is contained in:
Moritz Halbritter
2025-10-13 13:44:25 +02:00
parent 3c555a2f89
commit b56d9c6b08
5 changed files with 36 additions and 3 deletions
@@ -45,3 +45,7 @@ dependencies {
testRuntimeOnly("jakarta.servlet:jakarta.servlet-api")
testRuntimeOnly("org.springframework:spring-webflux")
}
tasks.named("compileTestJava") {
options.nullability.checking = "tests"
}
@@ -81,12 +81,15 @@ class RestTemplateBuilderTests {
private final RestTemplateBuilder builder = new RestTemplateBuilder();
@Mock
@SuppressWarnings("NullAway.Init")
private HttpMessageConverter<Object> 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<HttpMessageConverter<?>>) 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<HttpMessageConverter<?>>) 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<ClientHttpRequestInterceptor>) 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<ClientHttpRequestInterceptor>) null))
@@ -248,6 +259,7 @@ class RestTemplateBuilderTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void requestFactoryClassWhenFactoryIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.builder.requestFactory((Class<ClientHttpRequestFactory>) null))
@@ -267,6 +279,7 @@ class RestTemplateBuilderTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void requestFactoryWhenSupplierIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.builder.requestFactory((Supplier<ClientHttpRequestFactory>) 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<RestTemplateCustomizer>) 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<RestTemplateCustomizer>) 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 {
@@ -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");
@@ -52,12 +52,15 @@ class AutoConfiguredRestClientSslTests {
.withConnectTimeout(Duration.ofSeconds(30));
@Mock
@SuppressWarnings("NullAway.Init")
private SslBundles sslBundles;
@Mock
@SuppressWarnings("NullAway.Init")
private ClientHttpRequestFactoryBuilder<ClientHttpRequestFactory> factoryBuilder;
@Mock
@SuppressWarnings("NullAway.Init")
private ClientHttpRequestFactory factory;
private AutoConfiguredRestClientSsl restClientSsl;
@@ -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;
}
}