Add nullability annotations to tests in module/spring-boot-data-rest

See gh-47263
This commit is contained in:
Moritz Halbritter 2025-09-19 12:57:34 +02:00
parent 266f9c44e0
commit 1023ed65a6
2 changed files with 32 additions and 14 deletions

View File

@ -43,3 +43,7 @@ dependencies {
testRuntimeOnly("com.h2database:h2")
testRuntimeOnly("com.zaxxer:HikariCP")
}
tasks.named("compileTestJava") {
options.nullability.checking = "tests"
}

View File

@ -19,6 +19,7 @@ package org.springframework.boot.data.rest.autoconfigure;
import java.net.URI;
import java.text.SimpleDateFormat;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import tools.jackson.databind.json.JsonMapper;
@ -57,29 +58,29 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class DataRestAutoConfigurationTests {
private AnnotationConfigServletWebApplicationContext context;
private @Nullable AnnotationConfigServletWebApplicationContext context;
@AfterEach
void tearDown() {
if (this.context != null) {
this.context.close();
if (getContext() != null) {
getContext().close();
}
}
@Test
void testDefaultRepositoryConfiguration() {
load(TestConfiguration.class);
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
assertThat(getContext().getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
}
@Test
void testWithCustomBasePath() {
load(TestConfiguration.class, "spring.data.rest.base-path:foo");
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
assertThat(getContext().getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
RepositoryRestConfiguration bean = getContext().getBean(RepositoryRestConfiguration.class);
URI expectedUri = URI.create("/foo");
assertThat(bean.getBasePath()).as("Custom basePath not set").isEqualTo(expectedUri);
BaseUri baseUri = this.context.getBean(BaseUri.class);
BaseUri baseUri = getContext().getBean(BaseUri.class);
assertThat(expectedUri).as("Custom basePath has not been applied to BaseUri bean").isEqualTo(baseUri.getUri());
}
@ -91,8 +92,8 @@ class DataRestAutoConfigurationTests {
"spring.data.rest.default-media-type:application/my-json",
"spring.data.rest.return-body-on-create:false", "spring.data.rest.return-body-on-update:false",
"spring.data.rest.enable-enum-translation:true");
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
assertThat(getContext().getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
RepositoryRestConfiguration bean = getContext().getBean(RepositoryRestConfiguration.class);
assertThat(bean.getDefaultPageSize()).isEqualTo(42);
assertThat(bean.getMaxPageSize()).isEqualTo(78);
assertThat(bean.getPageParamName()).isEqualTo("_page");
@ -100,17 +101,24 @@ class DataRestAutoConfigurationTests {
assertThat(bean.getSortParamName()).isEqualTo("_sort");
assertThat(bean.getRepositoryDetectionStrategy()).isEqualTo(RepositoryDetectionStrategies.VISIBILITY);
assertThat(bean.getDefaultMediaType()).isEqualTo(MediaType.parseMediaType("application/my-json"));
assertReturnBody(bean);
assertThat(bean.isEnableEnumTranslation()).isTrue();
}
// https://github.com/spring-projects/spring-data-rest/issues/2515
// https://github.com/spring-projects/spring-data-rest/issues/2516
@SuppressWarnings("NullAway")
private void assertReturnBody(RepositoryRestConfiguration bean) {
assertThat(bean.returnBodyOnCreate(null)).isFalse();
assertThat(bean.returnBodyOnUpdate(null)).isFalse();
assertThat(bean.isEnableEnumTranslation()).isTrue();
}
@Test
void testWithCustomConfigurer() {
load(TestConfigurationWithConfigurer.class, "spring.data.rest.detection-strategy=visibility",
"spring.data.rest.default-media-type:application/my-json");
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
assertThat(getContext().getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
RepositoryRestConfiguration bean = getContext().getBean(RepositoryRestConfiguration.class);
assertThat(bean.getRepositoryDetectionStrategy()).isEqualTo(RepositoryDetectionStrategies.ALL);
assertThat(bean.getDefaultMediaType()).isEqualTo(MediaType.parseMediaType("application/my-custom-json"));
assertThat(bean.getMaxPageSize()).isEqualTo(78);
@ -119,8 +127,8 @@ class DataRestAutoConfigurationTests {
@Test
void backOffWithCustomConfiguration() {
load(TestConfigurationWithRestMvcConfig.class, "spring.data.rest.base-path:foo");
assertThat(this.context.getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
assertThat(getContext().getBean(RepositoryRestMvcConfiguration.class)).isNotNull();
RepositoryRestConfiguration bean = getContext().getBean(RepositoryRestConfiguration.class);
assertThat(bean.getBasePath()).isEqualTo(URI.create(""));
}
@ -133,6 +141,12 @@ class DataRestAutoConfigurationTests {
this.context = applicationContext;
}
private AnnotationConfigServletWebApplicationContext getContext() {
AnnotationConfigServletWebApplicationContext context = this.context;
assertThat(context).isNotNull();
return context;
}
@Configuration(proxyBeanMethods = false)
@Import(EmbeddedDataSourceConfiguration.class)
@ImportAutoConfiguration({ HibernateJpaAutoConfiguration.class, DataJpaRepositoriesAutoConfiguration.class,