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

See gh-47263
This commit is contained in:
Moritz Halbritter
2025-10-13 13:44:25 +02:00
parent b56d9c6b08
commit 558183e0fe
10 changed files with 35 additions and 16 deletions
@@ -40,3 +40,7 @@ dependencies {
testRuntimeOnly("ch.qos.logback:logback-classic")
}
tasks.named("compileTestJava") {
options.nullability.checking = "tests"
}
@@ -56,6 +56,7 @@ class MockServerRestClientCustomizerTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void createWhenExpectationManagerClassIsNullShouldThrowException() {
Class<? extends RequestExpectationManager> expectationManager = null;
assertThatIllegalArgumentException().isThrownBy(() -> new MockServerRestClientCustomizer(expectationManager))
@@ -63,6 +64,7 @@ class MockServerRestClientCustomizerTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void createWhenExpectationManagerSupplierIsNullShouldThrowException() {
Supplier<? extends RequestExpectationManager> expectationManagerSupplier = null;
assertThatIllegalArgumentException()
@@ -59,6 +59,7 @@ class MockServerRestTemplateCustomizerTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void createWhenExpectationManagerClassIsNullShouldThrowException() {
Class<? extends RequestExpectationManager> expectationManager = null;
assertThatIllegalArgumentException().isThrownBy(() -> new MockServerRestTemplateCustomizer(expectationManager))
@@ -66,6 +67,7 @@ class MockServerRestTemplateCustomizerTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void createWhenExpectationManagerSupplierIsNullShouldThrowException() {
Supplier<? extends RequestExpectationManager> expectationManagerSupplier = null;
assertThatIllegalArgumentException()
@@ -55,6 +55,7 @@ class RootUriRequestExpectationManagerTests {
private final String uri = "https://example.com";
@Mock
@SuppressWarnings("NullAway.Init")
private RequestExpectationManager delegate;
private RootUriRequestExpectationManager manager;
@@ -65,12 +66,14 @@ class RootUriRequestExpectationManagerTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void createWhenRootUriIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new RootUriRequestExpectationManager(null, this.delegate))
.withMessageContaining("'rootUri' must not be null");
}
@Test
@SuppressWarnings("NullAway") // Test null check
void createWhenExpectationManagerIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new RootUriRequestExpectationManager(this.uri, null))
.withMessageContaining("'expectationManager' must not be null");
@@ -16,6 +16,8 @@
package org.springframework.boot.restclient.test.autoconfigure;
import org.jspecify.annotations.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClient.Builder;
@@ -41,7 +43,7 @@ public class AnotherExampleRestClientService {
return this.builder;
}
public String test() {
public @Nullable String test() {
return this.restClient.get().uri("/test").retrieve().toEntity(String.class).getBody();
}
@@ -16,6 +16,8 @@
package org.springframework.boot.restclient.test.autoconfigure;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.restclient.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@@ -38,7 +40,7 @@ public class AnotherExampleRestTemplateService {
return this.restTemplate;
}
public String test() {
public @Nullable String test() {
return this.restTemplate.getForEntity("/test", String.class).getBody();
}
@@ -16,6 +16,8 @@
package org.springframework.boot.restclient.test.autoconfigure;
import org.jspecify.annotations.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClient.Builder;
@@ -42,7 +44,7 @@ public class ExampleRestClientService {
return this.builder;
}
public String test() {
public @Nullable String test() {
return this.restClient.get().uri("/test").retrieve().toEntity(String.class).getBody();
}
@@ -16,6 +16,8 @@
package org.springframework.boot.restclient.test.autoconfigure;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.restclient.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@@ -39,7 +41,7 @@ public class ExampleRestTemplateService {
return this.restTemplate;
}
public String test() {
public @Nullable String test() {
return this.restTemplate.getForEntity("/test", String.class).getBody();
}
@@ -58,17 +58,17 @@ class RestClientTestRestClientTwoComponentsIntegrationTests {
@Test
void client1RestCallViaCustomizer() {
this.customizer.getServer(this.client1.getRestClientBuilder())
.expect(requestTo(uri("/test")))
.andRespond(withSuccess("hello", MediaType.TEXT_HTML));
MockRestServiceServer server = this.customizer.getServer(this.client1.getRestClientBuilder());
assertThat(server).isNotNull();
server.expect(requestTo(uri("/test"))).andRespond(withSuccess("hello", MediaType.TEXT_HTML));
assertThat(this.client1.test()).isEqualTo("hello");
}
@Test
void client2RestCallViaCustomizer() {
this.customizer.getServer(this.client2.getRestClientBuilder())
.expect(requestTo(uri("/test")))
.andRespond(withSuccess("there", MediaType.TEXT_HTML));
MockRestServiceServer server = this.customizer.getServer(this.client2.getRestClientBuilder());
assertThat(server).isNotNull();
server.expect(requestTo(uri("/test"))).andRespond(withSuccess("there", MediaType.TEXT_HTML));
assertThat(this.client2.test()).isEqualTo("there");
}
@@ -58,17 +58,17 @@ class RestClientTestRestTemplateTwoComponentsIntegrationTests {
@Test
void client1RestCallViaCustomizer() {
this.customizer.getServer(this.client1.getRestTemplate())
.expect(requestTo("/test"))
.andRespond(withSuccess("hello", MediaType.TEXT_HTML));
MockRestServiceServer server = this.customizer.getServer(this.client1.getRestTemplate());
assertThat(server).isNotNull();
server.expect(requestTo("/test")).andRespond(withSuccess("hello", MediaType.TEXT_HTML));
assertThat(this.client1.test()).isEqualTo("hello");
}
@Test
void client2RestCallViaCustomizer() {
this.customizer.getServer(this.client2.getRestTemplate())
.expect(requestTo("/test"))
.andRespond(withSuccess("there", MediaType.TEXT_HTML));
MockRestServiceServer server = this.customizer.getServer(this.client2.getRestTemplate());
assertThat(server).isNotNull();
server.expect(requestTo("/test")).andRespond(withSuccess("there", MediaType.TEXT_HTML));
assertThat(this.client2.test()).isEqualTo("there");
}