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

See gh-47263
This commit is contained in:
Moritz Halbritter
2025-10-13 11:37:00 +02:00
parent 05b3f55049
commit 7dd6f93e91
4 changed files with 26 additions and 5 deletions
@@ -41,3 +41,7 @@ dependencies {
testRuntimeOnly("ch.qos.logback:logback-classic")
}
tasks.named("compileTestJava") {
options.nullability.checking = "tests"
}
@@ -19,6 +19,7 @@ package org.springframework.boot.persistence.autoconfigure;
import java.util.Collection;
import java.util.Collections;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
@@ -38,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/
class EntityScanPackagesTests {
private AnnotationConfigApplicationContext context;
private @Nullable AnnotationConfigApplicationContext context;
@AfterEach
void cleanup() {
@@ -67,6 +68,7 @@ class EntityScanPackagesTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void registerFromArrayWhenRegistryIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> EntityScanPackages.register(null))
.withMessageContaining("'registry' must not be null");
@@ -74,6 +76,7 @@ class EntityScanPackagesTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void registerFromArrayWhenPackageNamesIsNullShouldThrowException() {
this.context = new AnnotationConfigApplicationContext();
assertThatIllegalArgumentException()
@@ -82,6 +85,7 @@ class EntityScanPackagesTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void registerFromCollectionWhenRegistryIsNullShouldThrowException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> EntityScanPackages.register(null, Collections.emptyList()))
@@ -89,6 +93,7 @@ class EntityScanPackagesTests {
}
@Test
@SuppressWarnings("NullAway") // Test null check
void registerFromCollectionWhenPackageNamesIsNullShouldThrowException() {
this.context = new AnnotationConfigApplicationContext();
assertThatIllegalArgumentException()
@@ -51,6 +51,7 @@ import static org.mockito.Mockito.mock;
class EntityScannerTests {
@Test
@SuppressWarnings("NullAway") // Test null check
void createWhenContextIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new EntityScanner(null))
.withMessageContaining("'context' must not be null");
@@ -34,6 +34,7 @@ import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
@@ -60,7 +61,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/
class PersistenceExceptionTranslationAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
private @Nullable AnnotationConfigApplicationContext context;
@AfterEach
void close() {
@@ -105,7 +106,10 @@ class PersistenceExceptionTranslationAutoConfigurationTests {
@WithMetaInfPersistenceXmlResource
void persistOfNullThrowsIllegalArgumentExceptionWithoutExceptionTranslation() {
this.context = new AnnotationConfigApplicationContext(JpaConfiguration.class, TestConfiguration.class);
assertThatIllegalArgumentException().isThrownBy(() -> this.context.getBean(TestRepository.class).doSomething());
assertThatIllegalArgumentException().isThrownBy(() -> {
assertThat(this.context).isNotNull();
this.context.getBean(TestRepository.class).doSomething();
});
}
@Test
@@ -113,8 +117,10 @@ class PersistenceExceptionTranslationAutoConfigurationTests {
void persistOfNullThrowsInvalidDataAccessApiUsageExceptionWithExceptionTranslation() {
this.context = new AnnotationConfigApplicationContext(JpaConfiguration.class, TestConfiguration.class,
PersistenceExceptionTranslationAutoConfiguration.class);
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
.isThrownBy(() -> this.context.getBean(TestRepository.class).doSomething());
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> {
assertThat(this.context).isNotNull();
this.context.getBean(TestRepository.class).doSomething();
});
}
@Configuration(proxyBeanMethods = false)
@@ -195,18 +201,23 @@ class PersistenceExceptionTranslationAutoConfigurationTests {
@Id
@GeneratedValue
@SuppressWarnings("NullAway.Init")
private Long id;
@Column(nullable = false)
@SuppressWarnings("NullAway.Init")
private String name;
@Column(nullable = false)
@SuppressWarnings("NullAway.Init")
private String state;
@Column(nullable = false)
@SuppressWarnings("NullAway.Init")
private String country;
@Column(nullable = false)
@SuppressWarnings("NullAway.Init")
private String map;
protected City() {