Skip to content

Commit 04aefab

Browse files
committed
Add nullability annotations to tests in module/spring-boot-resttestclient
See gh-47263
1 parent 56795f9 commit 04aefab

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

module/spring-boot-resttestclient/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ dependencies {
4343

4444
testRuntimeOnly("ch.qos.logback:logback-classic")
4545
}
46+
47+
tasks.named("compileTestJava") {
48+
options.nullability.checking = "tests"
49+
}

module/spring-boot-resttestclient/src/test/java/org/springframework/boot/resttestclient/TestRestTemplateTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.hc.client5.http.impl.classic.RedirectExec;
3131
import org.apache.hc.client5.http.protocol.RedirectStrategy;
3232
import org.assertj.core.extractor.Extractors;
33+
import org.jspecify.annotations.Nullable;
3334
import org.junit.jupiter.api.Test;
3435

3536
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
@@ -203,13 +204,14 @@ private RequestConfig getRequestConfig(TestRestTemplate template) {
203204
return (RequestConfig) Extractors.byName("httpClient.defaultConfig").apply(requestFactory);
204205
}
205206

206-
private RedirectStrategy getRedirectStrategy(RestTemplateBuilder builder, HttpClientOption... httpClientOptions) {
207+
private @Nullable RedirectStrategy getRedirectStrategy(@Nullable RestTemplateBuilder builder,
208+
HttpClientOption... httpClientOptions) {
207209
builder = (builder != null) ? builder : new RestTemplateBuilder();
208210
TestRestTemplate template = new TestRestTemplate(builder, null, null, httpClientOptions);
209211
return getRedirectStrategy(template);
210212
}
211213

212-
private RedirectStrategy getRedirectStrategy(TestRestTemplate template) {
214+
private @Nullable RedirectStrategy getRedirectStrategy(TestRestTemplate template) {
213215
ClientHttpRequestFactory requestFactory = template.getRestTemplate().getRequestFactory();
214216
Object chain = Extractors.byName("httpClient.execChain").apply(requestFactory);
215217
while (chain != null) {
@@ -233,7 +235,9 @@ private boolean isDontFollowStrategy(RedirectStrategy redirectStrategy) {
233235
private HttpClient getJdkHttpClient(TestRestTemplate template) {
234236
JdkClientHttpRequestFactory requestFactory = (JdkClientHttpRequestFactory) template.getRestTemplate()
235237
.getRequestFactory();
236-
return (HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient");
238+
HttpClient httpClient = (HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient");
239+
assertThat(httpClient).isNotNull();
240+
return httpClient;
237241
}
238242

239243
@Test
@@ -459,10 +463,11 @@ private void verifyRelativeUriHandling(TestRestTemplateCallback callback) throws
459463
then(requestFactory).should().createRequest(eq(absoluteUri), any(HttpMethod.class));
460464
}
461465

462-
private void assertBasicAuthorizationCredentials(TestRestTemplate testRestTemplate, String username,
463-
String password) {
466+
private void assertBasicAuthorizationCredentials(TestRestTemplate testRestTemplate, @Nullable String username,
467+
@Nullable String password) {
464468
ClientHttpRequest request = ReflectionTestUtils.invokeMethod(testRestTemplate.getRestTemplate(),
465469
"createRequest", URI.create("http://localhost"), HttpMethod.POST);
470+
assertThat(request).isNotNull();
466471
if (username == null) {
467472
assertThat(request.getHeaders().headerNames()).doesNotContain(HttpHeaders.AUTHORIZATION);
468473
}

0 commit comments

Comments
 (0)