Skip to content

Commit b56d9c6

Browse files
committed
Add nullability annotations to tests in module/spring-boot-restclient
See gh-47263
1 parent 3c555a2 commit b56d9c6

5 files changed

Lines changed: 36 additions & 3 deletions

File tree

module/spring-boot-restclient/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ dependencies {
4545
testRuntimeOnly("jakarta.servlet:jakarta.servlet-api")
4646
testRuntimeOnly("org.springframework:spring-webflux")
4747
}
48+
49+
tasks.named("compileTestJava") {
50+
options.nullability.checking = "tests"
51+
}

module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RestTemplateBuilderTests.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,15 @@ class RestTemplateBuilderTests {
8181
private final RestTemplateBuilder builder = new RestTemplateBuilder();
8282

8383
@Mock
84+
@SuppressWarnings("NullAway.Init")
8485
private HttpMessageConverter<Object> messageConverter;
8586

8687
@Mock
88+
@SuppressWarnings("NullAway.Init")
8789
private ClientHttpRequestInterceptor interceptor;
8890

8991
@Test
92+
@SuppressWarnings("NullAway") // Test null check
9093
void createWhenCustomizersAreNullShouldThrowException() {
9194
RestTemplateCustomizer[] customizers = null;
9295
assertThatIllegalArgumentException().isThrownBy(() -> new RestTemplateBuilder(customizers))
@@ -134,13 +137,15 @@ void rootUriShouldApplyAfterUriTemplateHandler() {
134137
}
135138

136139
@Test
140+
@SuppressWarnings("NullAway") // Test null check
137141
void messageConvertersWhenConvertersAreNullShouldThrowException() {
138142
assertThatIllegalArgumentException()
139143
.isThrownBy(() -> this.builder.messageConverters((HttpMessageConverter<?>[]) null))
140144
.withMessageContaining("'messageConverters' must not be null");
141145
}
142146

143147
@Test
148+
@SuppressWarnings("NullAway") // Test null check
144149
void messageConvertersCollectionWhenConvertersAreNullShouldThrowException() {
145150
assertThatIllegalArgumentException()
146151
.isThrownBy(() -> this.builder.messageConverters((Set<HttpMessageConverter<?>>) null))
@@ -162,13 +167,15 @@ void messageConvertersShouldReplaceExisting() {
162167
}
163168

164169
@Test
170+
@SuppressWarnings("NullAway") // Test null check
165171
void additionalMessageConvertersWhenConvertersAreNullShouldThrowException() {
166172
assertThatIllegalArgumentException()
167173
.isThrownBy(() -> this.builder.additionalMessageConverters((HttpMessageConverter<?>[]) null))
168174
.withMessageContaining("'messageConverters' must not be null");
169175
}
170176

171177
@Test
178+
@SuppressWarnings("NullAway") // Test null check
172179
void additionalMessageConvertersCollectionWhenConvertersAreNullShouldThrowException() {
173180
assertThatIllegalArgumentException()
174181
.isThrownBy(() -> this.builder.additionalMessageConverters((Set<HttpMessageConverter<?>>) null))
@@ -199,13 +206,15 @@ void defaultMessageConvertersShouldClearExisting() {
199206
}
200207

201208
@Test
209+
@SuppressWarnings("NullAway") // Test null check
202210
void interceptorsWhenInterceptorsAreNullShouldThrowException() {
203211
assertThatIllegalArgumentException()
204212
.isThrownBy(() -> this.builder.interceptors((ClientHttpRequestInterceptor[]) null))
205213
.withMessageContaining("'interceptors' must not be null");
206214
}
207215

208216
@Test
217+
@SuppressWarnings("NullAway") // Test null check
209218
void interceptorsCollectionWhenInterceptorsAreNullShouldThrowException() {
210219
assertThatIllegalArgumentException()
211220
.isThrownBy(() -> this.builder.interceptors((Set<ClientHttpRequestInterceptor>) null))
@@ -227,13 +236,15 @@ void interceptorsShouldReplaceExisting() {
227236
}
228237

229238
@Test
239+
@SuppressWarnings("NullAway") // Test null check
230240
void additionalInterceptorsWhenInterceptorsAreNullShouldThrowException() {
231241
assertThatIllegalArgumentException()
232242
.isThrownBy(() -> this.builder.additionalInterceptors((ClientHttpRequestInterceptor[]) null))
233243
.withMessageContaining("'interceptors' must not be null");
234244
}
235245

236246
@Test
247+
@SuppressWarnings("NullAway") // Test null check
237248
void additionalInterceptorsCollectionWhenInterceptorsAreNullShouldThrowException() {
238249
assertThatIllegalArgumentException()
239250
.isThrownBy(() -> this.builder.additionalInterceptors((Set<ClientHttpRequestInterceptor>) null))
@@ -248,6 +259,7 @@ void additionalInterceptorsShouldAddToExisting() {
248259
}
249260

250261
@Test
262+
@SuppressWarnings("NullAway") // Test null check
251263
void requestFactoryClassWhenFactoryIsNullShouldThrowException() {
252264
assertThatIllegalArgumentException()
253265
.isThrownBy(() -> this.builder.requestFactory((Class<ClientHttpRequestFactory>) null))
@@ -267,6 +279,7 @@ void requestFactoryPackagePrivateClassShouldApply() {
267279
}
268280

269281
@Test
282+
@SuppressWarnings("NullAway") // Test null check
270283
void requestFactoryWhenSupplierIsNullShouldThrowException() {
271284
assertThatIllegalArgumentException()
272285
.isThrownBy(() -> this.builder.requestFactory((Supplier<ClientHttpRequestFactory>) null))
@@ -281,6 +294,7 @@ void requestFactoryShouldApply() {
281294
}
282295

283296
@Test
297+
@SuppressWarnings("NullAway") // Test null check
284298
void uriTemplateHandlerWhenHandlerIsNullShouldThrowException() {
285299
assertThatIllegalArgumentException().isThrownBy(() -> this.builder.uriTemplateHandler(null))
286300
.withMessageContaining("'uriTemplateHandler' must not be null");
@@ -294,6 +308,7 @@ void uriTemplateHandlerShouldApply() {
294308
}
295309

296310
@Test
311+
@SuppressWarnings("NullAway") // Test null check
297312
void errorHandlerWhenHandlerIsNullShouldThrowException() {
298313
assertThatIllegalArgumentException().isThrownBy(() -> this.builder.errorHandler(null))
299314
.withMessageContaining("'errorHandler' must not be null");
@@ -366,12 +381,14 @@ void additionalRequestCustomizersAddsCustomizers() {
366381
}
367382

368383
@Test
384+
@SuppressWarnings("NullAway") // Test null check
369385
void customizersWhenCustomizersAreNullShouldThrowException() {
370386
assertThatIllegalArgumentException().isThrownBy(() -> this.builder.customizers((RestTemplateCustomizer[]) null))
371387
.withMessageContaining("'customizers' must not be null");
372388
}
373389

374390
@Test
391+
@SuppressWarnings("NullAway") // Test null check
375392
void customizersCollectionWhenCustomizersAreNullShouldThrowException() {
376393
assertThatIllegalArgumentException()
377394
.isThrownBy(() -> this.builder.customizers((Set<RestTemplateCustomizer>) null))
@@ -405,13 +422,15 @@ void customizersShouldReplaceExisting() {
405422
}
406423

407424
@Test
425+
@SuppressWarnings("NullAway") // Test null check
408426
void additionalCustomizersWhenCustomizersAreNullShouldThrowException() {
409427
assertThatIllegalArgumentException()
410428
.isThrownBy(() -> this.builder.additionalCustomizers((RestTemplateCustomizer[]) null))
411429
.withMessageContaining("'customizers' must not be null");
412430
}
413431

414432
@Test
433+
@SuppressWarnings("NullAway") // Test null check
415434
void additionalCustomizersCollectionWhenCustomizersAreNullShouldThrowException() {
416435
assertThatIllegalArgumentException()
417436
.isThrownBy(() -> this.builder.additionalCustomizers((Set<RestTemplateCustomizer>) null))
@@ -486,8 +505,10 @@ void configureRedirects() {
486505
}
487506

488507
private ClientHttpRequest createRequest(RestTemplate template) {
489-
return ReflectionTestUtils.invokeMethod(template, "createRequest", URI.create("http://localhost"),
490-
HttpMethod.GET);
508+
ClientHttpRequest request = ReflectionTestUtils.invokeMethod(template, "createRequest",
509+
URI.create("http://localhost"), HttpMethod.GET);
510+
assertThat(request).isNotNull();
511+
return request;
491512
}
492513

493514
static class RestTemplateSubclass extends RestTemplate {

module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/RootUriTemplateHandlerTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class RootUriTemplateHandlerTests {
4848
private URI uri;
4949

5050
@Mock
51+
@SuppressWarnings("NullAway.Init")
5152
public UriTemplateHandler delegate;
5253

5354
public UriTemplateHandler handler;
@@ -59,13 +60,15 @@ void setup() throws URISyntaxException {
5960
}
6061

6162
@Test
63+
@SuppressWarnings("NullAway") // Test null check
6264
void createWithNullRootUriShouldThrowException() {
6365
assertThatIllegalArgumentException()
6466
.isThrownBy(() -> new RootUriTemplateHandler((String) null, mock(UriTemplateHandler.class)))
6567
.withMessageContaining("'rootUri' must not be null");
6668
}
6769

6870
@Test
71+
@SuppressWarnings("NullAway") // Test null check
6972
void createWithNullHandlerShouldThrowException() {
7073
assertThatIllegalArgumentException().isThrownBy(() -> new RootUriTemplateHandler("https://example.com", null))
7174
.withMessageContaining("'handler' must not be null");

module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/AutoConfiguredRestClientSslTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ class AutoConfiguredRestClientSslTests {
5252
.withConnectTimeout(Duration.ofSeconds(30));
5353

5454
@Mock
55+
@SuppressWarnings("NullAway.Init")
5556
private SslBundles sslBundles;
5657

5758
@Mock
59+
@SuppressWarnings("NullAway.Init")
5860
private ClientHttpRequestFactoryBuilder<ClientHttpRequestFactory> factoryBuilder;
5961

6062
@Mock
63+
@SuppressWarnings("NullAway.Init")
6164
private ClientHttpRequestFactory factory;
6265

6366
private AutoConfiguredRestClientSsl restClientSsl;

module/spring-boot-restclient/src/test/java/org/springframework/boot/restclient/autoconfigure/service/HttpServiceClientAutoConfigurationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ private MockRestServiceServer addMock(HttpServiceGroup group, Builder client) {
222222
}
223223

224224
MockRestServiceServer getMock(String name) {
225-
return this.mocks.get(name);
225+
MockRestServiceServer mock = this.mocks.get(name);
226+
assertThat(mock).isNotNull();
227+
return mock;
226228
}
227229

228230
}

0 commit comments

Comments
 (0)