Add nullability annotations to tests in core/spring-boot-testcontainers
See gh-47263
This commit is contained in:
parent
9bb62a074b
commit
23c9b6510b
|
@ -61,3 +61,11 @@ dependencies {
|
|||
dockerTest {
|
||||
jvmArgs += "--add-opens=java.base/java.util.concurrent=ALL-UNNAMED"
|
||||
}
|
||||
|
||||
tasks.named("compileTestJava") {
|
||||
options.nullability.checking = "tests"
|
||||
}
|
||||
|
||||
tasks.named("compileDockerTestJava") {
|
||||
options.nullability.checking = "tests"
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.springframework.boot.testcontainers;
|
|||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.Container;
|
||||
|
@ -43,7 +44,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
|||
@DisabledIfDockerUnavailable
|
||||
class ImportTestcontainersTests {
|
||||
|
||||
private AnnotationConfigApplicationContext applicationContext;
|
||||
private @Nullable AnnotationConfigApplicationContext applicationContext;
|
||||
|
||||
@AfterEach
|
||||
void teardown() {
|
||||
|
@ -143,7 +144,7 @@ class ImportTestcontainersTests {
|
|||
@ImportTestcontainers
|
||||
static class NullContainer {
|
||||
|
||||
static PostgreSQLContainer<?> container = null;
|
||||
static @Nullable PostgreSQLContainer<?> container = null;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,8 @@ import org.testcontainers.lifecycle.Startables;
|
|||
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* JUnit extension used by reset startables.
|
||||
*
|
||||
|
@ -46,8 +48,11 @@ class ResetStartablesExtension implements BeforeEachCallback, AfterEachCallback
|
|||
private void reset() {
|
||||
try {
|
||||
Object executor = ReflectionTestUtils.getField(Startables.class, "EXECUTOR");
|
||||
assertThat(executor).isNotNull();
|
||||
Object threadFactory = ReflectionTestUtils.getField(executor, "threadFactory");
|
||||
assertThat(threadFactory).isNotNull();
|
||||
AtomicLong counter = (AtomicLong) ReflectionTestUtils.getField(threadFactory, "COUNTER");
|
||||
assertThat(counter).isNotNull();
|
||||
counter.set(0);
|
||||
}
|
||||
catch (InaccessibleObjectException ex) {
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.testcontainers.lifecycle;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
@ -74,13 +75,13 @@ class TestcontainersImportWithPropertiesInjectedIntoLoadTimeWeaverAwareBeanInteg
|
|||
@ConfigurationProperties("spring.datasource")
|
||||
public static class MockDataSourceProperties {
|
||||
|
||||
private String url;
|
||||
private @Nullable String url;
|
||||
|
||||
public String getUrl() {
|
||||
public @Nullable String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
public void setUrl(@Nullable String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import com.redis.testcontainers.RedisContainer;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
@ -166,7 +167,9 @@ class TestcontainersLifecycleOrderWithScopeIntegrationTests {
|
|||
public Object remove(String name) {
|
||||
synchronized (this) {
|
||||
Object removed = this.instances.remove(name);
|
||||
this.destructors.get(name).forEach(Runnable::run);
|
||||
List<Runnable> destructor = this.destructors.get(name);
|
||||
assertThat(destructor).isNotNull();
|
||||
destructor.forEach(Runnable::run);
|
||||
this.destructors.remove(name);
|
||||
return removed;
|
||||
}
|
||||
|
@ -178,12 +181,12 @@ class TestcontainersLifecycleOrderWithScopeIntegrationTests {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Object resolveContextualObject(String key) {
|
||||
public @Nullable Object resolveContextualObject(String key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConversationId() {
|
||||
public @Nullable String getConversationId() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.lifecycle.Startable;
|
||||
|
@ -169,7 +170,7 @@ class TestcontainersStartupTests {
|
|||
|
||||
private int index;
|
||||
|
||||
private String threadName;
|
||||
private @Nullable String threadName;
|
||||
|
||||
TestStartable() {
|
||||
super("test");
|
||||
|
@ -206,7 +207,7 @@ class TestcontainersStartupTests {
|
|||
return this.index;
|
||||
}
|
||||
|
||||
String getThreadName() {
|
||||
@Nullable String getThreadName() {
|
||||
return this.threadName;
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.util.Collections;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
|
@ -125,6 +126,7 @@ class ContainerConnectionDetailsFactoryTests {
|
|||
void getContainerWhenNotInitializedThrowsException() {
|
||||
TestContainerConnectionDetailsFactory factory = new TestContainerConnectionDetailsFactory();
|
||||
TestDatabaseConnectionDetails connectionDetails = getConnectionDetails(factory, this.source);
|
||||
assertThat(connectionDetails).isNotNull();
|
||||
assertThatIllegalStateException().isThrownBy(connectionDetails::callGetContainer)
|
||||
.withMessage("Container cannot be obtained before the connection details bean has been initialized");
|
||||
}
|
||||
|
@ -133,6 +135,7 @@ class ContainerConnectionDetailsFactoryTests {
|
|||
void getContainerWhenInitializedReturnsSuppliedContainer() throws Exception {
|
||||
TestContainerConnectionDetailsFactory factory = new TestContainerConnectionDetailsFactory();
|
||||
TestDatabaseConnectionDetails connectionDetails = getConnectionDetails(factory, this.source);
|
||||
assertThat(connectionDetails).isNotNull();
|
||||
connectionDetails.afterPropertiesSet();
|
||||
assertThat(connectionDetails.callGetContainer()).isSameAs(this.container);
|
||||
}
|
||||
|
@ -144,13 +147,14 @@ class ContainerConnectionDetailsFactoryTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("NullAway") // Test null check
|
||||
void creatingFactoryWithNullNamesThrows() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new TestContainerConnectionDetailsFactory((List<String>) null));
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private TestDatabaseConnectionDetails getConnectionDetails(ConnectionDetailsFactory<?, ?> factory,
|
||||
private @Nullable TestDatabaseConnectionDetails getConnectionDetails(ConnectionDetailsFactory<?, ?> factory,
|
||||
ContainerConnectionSource<?> source) {
|
||||
return (TestDatabaseConnectionDetails) ((ConnectionDetailsFactory) factory).getConnectionDetails(source);
|
||||
}
|
||||
|
@ -165,7 +169,7 @@ class ContainerConnectionDetailsFactoryTests {
|
|||
this(ANY_CONNECTION_NAME);
|
||||
}
|
||||
|
||||
TestContainerConnectionDetailsFactory(String connectionName) {
|
||||
TestContainerConnectionDetailsFactory(@Nullable String connectionName) {
|
||||
super(connectionName);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
|
||||
package org.springframework.boot.testcontainers.service.connection;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.origin.Origin;
|
||||
|
@ -34,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
|||
class FieldOriginTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("NullAway") // Test null check
|
||||
void createWhenFieldIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new FieldOrigin(null))
|
||||
.withMessage("'field' must not be null");
|
||||
|
@ -41,24 +45,30 @@ class FieldOriginTests {
|
|||
|
||||
@Test
|
||||
void equalsAndHashCode() {
|
||||
Origin o1 = new FieldOrigin(ReflectionUtils.findField(Fields.class, "one"));
|
||||
Origin o2 = new FieldOrigin(ReflectionUtils.findField(Fields.class, "one"));
|
||||
Origin o3 = new FieldOrigin(ReflectionUtils.findField(Fields.class, "two"));
|
||||
Origin o1 = new FieldOrigin(findField("one"));
|
||||
Origin o2 = new FieldOrigin(findField("one"));
|
||||
Origin o3 = new FieldOrigin(findField("two"));
|
||||
assertThat(o1).isEqualTo(o1).isEqualTo(o2).isNotEqualTo(o3);
|
||||
assertThat(o1).hasSameHashCodeAs(o2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringReturnsSensibleString() {
|
||||
Origin origin = new FieldOrigin(ReflectionUtils.findField(Fields.class, "one"));
|
||||
Origin origin = new FieldOrigin(findField("one"));
|
||||
assertThat(origin).hasToString("FieldOriginTests.Fields.one");
|
||||
}
|
||||
|
||||
private Field findField(String name) {
|
||||
Field field = ReflectionUtils.findField(Fields.class, name);
|
||||
assertThat(field).isNotNull();
|
||||
return field;
|
||||
}
|
||||
|
||||
static class Fields {
|
||||
|
||||
String one;
|
||||
@Nullable String one;
|
||||
|
||||
String two;
|
||||
@Nullable String two;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.boot.testcontainers.service.connection;
|
|||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -45,7 +46,8 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
|
||||
@Test
|
||||
void createContextCustomizerWhenNoServiceConnectionsReturnsCustomizerToApplyInitializer() {
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(NoServiceConnections.class, null);
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(NoServiceConnections.class,
|
||||
Collections.emptyList());
|
||||
assertThat(customizer).isNotNull();
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
int initialNumberOfPostProcessors = context.getBeanFactoryPostProcessors().size();
|
||||
|
@ -57,7 +59,7 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerWhenClassHasServiceConnectionsReturnsCustomizer() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(ServiceConnections.class, null);
|
||||
.createContextCustomizer(ServiceConnections.class, Collections.emptyList());
|
||||
assertThat(customizer).isNotNull();
|
||||
assertThat(customizer.getSources()).hasSize(2);
|
||||
}
|
||||
|
@ -65,7 +67,7 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerWhenEnclosingClassHasServiceConnectionsReturnsCustomizer() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(ServiceConnections.NestedClass.class, null);
|
||||
.createContextCustomizer(ServiceConnections.NestedClass.class, Collections.emptyList());
|
||||
assertThat(customizer).isNotNull();
|
||||
assertThat(customizer.getSources()).hasSize(3);
|
||||
}
|
||||
|
@ -73,7 +75,7 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerWhenInterfaceHasServiceConnectionsReturnsCustomizer() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(ServiceConnectionsInterface.class, null);
|
||||
.createContextCustomizer(ServiceConnectionsInterface.class, Collections.emptyList());
|
||||
assertThat(customizer).isNotNull();
|
||||
assertThat(customizer.getSources()).hasSize(2);
|
||||
}
|
||||
|
@ -81,7 +83,7 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerWhenSuperclassHasServiceConnectionsReturnsCustomizer() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(ServiceConnectionsSubclass.class, null);
|
||||
.createContextCustomizer(ServiceConnectionsSubclass.class, Collections.emptyList());
|
||||
assertThat(customizer).isNotNull();
|
||||
assertThat(customizer.getSources()).hasSize(2);
|
||||
}
|
||||
|
@ -89,7 +91,7 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerWhenImplementedInterfaceHasServiceConnectionsReturnsCustomizer() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(ServiceConnectionsImpl.class, null);
|
||||
.createContextCustomizer(ServiceConnectionsImpl.class, Collections.emptyList());
|
||||
assertThat(customizer).isNotNull();
|
||||
assertThat(customizer.getSources()).hasSize(2);
|
||||
}
|
||||
|
@ -97,15 +99,15 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerWhenInheritedImplementedInterfaceHasServiceConnectionsReturnsCustomizer() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(ServiceConnectionsImplSubclass.class, null);
|
||||
.createContextCustomizer(ServiceConnectionsImplSubclass.class, Collections.emptyList());
|
||||
assertThat(customizer).isNotNull();
|
||||
assertThat(customizer.getSources()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createContextCustomizerWhenClassHasNonStaticServiceConnectionFailsWithHelpfulException() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> this.factory.createContextCustomizer(NonStaticServiceConnection.class, null))
|
||||
assertThatIllegalStateException().isThrownBy(
|
||||
() -> this.factory.createContextCustomizer(NonStaticServiceConnection.class, Collections.emptyList()))
|
||||
.withMessage("@ServiceConnection field 'service' must be static");
|
||||
|
||||
}
|
||||
|
@ -113,7 +115,8 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerWhenClassHasAnnotationOnNonConnectionFieldFailsWithHelpfulException() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> this.factory.createContextCustomizer(ServiceConnectionOnWrongFieldType.class, null))
|
||||
.isThrownBy(() -> this.factory.createContextCustomizer(ServiceConnectionOnWrongFieldType.class,
|
||||
Collections.emptyList()))
|
||||
.withMessage("Field 'service2' in " + ServiceConnectionOnWrongFieldType.class.getName()
|
||||
+ " must be a org.testcontainers.containers.Container");
|
||||
}
|
||||
|
@ -121,7 +124,7 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerCreatesCustomizerSourceWithSensibleBeanNameSuffix() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(SingleServiceConnection.class, null);
|
||||
.createContextCustomizer(SingleServiceConnection.class, Collections.emptyList());
|
||||
ContainerConnectionSource<?> source = customizer.getSources().get(0);
|
||||
assertThat(source.getBeanNameSuffix()).isEqualTo("test");
|
||||
}
|
||||
|
@ -129,7 +132,7 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerCreatesCustomizerSourceWithSensibleOrigin() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(SingleServiceConnection.class, null);
|
||||
.createContextCustomizer(SingleServiceConnection.class, Collections.emptyList());
|
||||
ContainerConnectionSource<?> source = customizer.getSources().get(0);
|
||||
assertThat(source.getOrigin())
|
||||
.hasToString("ServiceConnectionContextCustomizerFactoryTests.SingleServiceConnection.service1");
|
||||
|
@ -138,7 +141,7 @@ class ServiceConnectionContextCustomizerFactoryTests {
|
|||
@Test
|
||||
void createContextCustomizerCreatesCustomizerSourceWithSensibleToString() {
|
||||
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
|
||||
.createContextCustomizer(SingleServiceConnection.class, null);
|
||||
.createContextCustomizer(SingleServiceConnection.class, Collections.emptyList());
|
||||
ContainerConnectionSource<?> source = customizer.getSources().get(0);
|
||||
assertThat(source).hasToString(
|
||||
"@ServiceConnection source for ServiceConnectionContextCustomizerFactoryTests.SingleServiceConnection.service1");
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.springframework.boot.testcontainers.service.connection;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -84,7 +85,9 @@ class ServiceConnectionContextCustomizerTests {
|
|||
then(beanFactory).should()
|
||||
.registerBeanDefinition(eq("testConnectionDetailsForTest"),
|
||||
ArgumentMatchers.<RootBeanDefinition>assertArg((beanDefinition) -> {
|
||||
assertThat(beanDefinition.getInstanceSupplier().get()).isSameAs(connectionDetails);
|
||||
Supplier<?> instanceSupplier = beanDefinition.getInstanceSupplier();
|
||||
assertThat(instanceSupplier).isNotNull();
|
||||
assertThat(instanceSupplier.get()).isSameAs(connectionDetails);
|
||||
assertThat(beanDefinition.getBeanClass()).isEqualTo(TestConnectionDetails.class);
|
||||
}));
|
||||
}
|
||||
|
@ -141,7 +144,7 @@ class ServiceConnectionContextCustomizerTests {
|
|||
|
||||
@Override
|
||||
public String getJdbcUrl() {
|
||||
return null;
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue