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