Support import of idomatic testcontainer declaration classes
Add an `@ImportTestcontainers` annotation which can be used to import idomatic testcontainer declaration classes. Closes gh-35245
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testcontainers;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.Container;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
import org.springframework.boot.testcontainers.beans.TestcontainerBeanDefinition;
|
||||
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
|
||||
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link ImportTestcontainers}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class ImportTestcontainersTests {
|
||||
|
||||
private AnnotationConfigApplicationContext applicationContext;
|
||||
|
||||
@AfterEach
|
||||
void teardown() {
|
||||
if (this.applicationContext != null) {
|
||||
this.applicationContext.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void importWithoutValueRegistersBeans() {
|
||||
this.applicationContext = new AnnotationConfigApplicationContext(ImportWithoutValue.class);
|
||||
String[] beanNames = this.applicationContext.getBeanNamesForType(PostgreSQLContainer.class);
|
||||
assertThat(beanNames).hasSize(1);
|
||||
assertThat(this.applicationContext.getBean(beanNames[0])).isSameAs(ImportWithoutValue.container);
|
||||
TestcontainerBeanDefinition beanDefinition = (TestcontainerBeanDefinition) this.applicationContext
|
||||
.getBeanDefinition(beanNames[0]);
|
||||
assertThat(beanDefinition.getContainerImageName()).isEqualTo(ImportWithoutValue.container.getDockerImageName());
|
||||
assertThat(beanDefinition.getAnnotations().isPresent(ContainerAnnotation.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void importWithValueRegistersBeans() {
|
||||
this.applicationContext = new AnnotationConfigApplicationContext(ImportWithValue.class);
|
||||
String[] beanNames = this.applicationContext.getBeanNamesForType(PostgreSQLContainer.class);
|
||||
assertThat(beanNames).hasSize(1);
|
||||
assertThat(this.applicationContext.getBean(beanNames[0])).isSameAs(ContainerDefinitions.container);
|
||||
TestcontainerBeanDefinition beanDefinition = (TestcontainerBeanDefinition) this.applicationContext
|
||||
.getBeanDefinition(beanNames[0]);
|
||||
assertThat(beanDefinition.getContainerImageName())
|
||||
.isEqualTo(ContainerDefinitions.container.getDockerImageName());
|
||||
assertThat(beanDefinition.getAnnotations().isPresent(ContainerAnnotation.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void importWhenHasNoContainerFieldsDoesNothing() {
|
||||
this.applicationContext = new AnnotationConfigApplicationContext(NoContainers.class);
|
||||
String[] beanNames = this.applicationContext.getBeanNamesForType(Container.class);
|
||||
assertThat(beanNames).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void importWhenHasNullContainerFieldThrowsException() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> this.applicationContext = new AnnotationConfigApplicationContext(NullContainer.class))
|
||||
.withMessage("Container field 'container' must not have a null value");
|
||||
}
|
||||
|
||||
@Test
|
||||
void importWhenHasNonStaticContainerFieldThrowsException() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(
|
||||
() -> this.applicationContext = new AnnotationConfigApplicationContext(NonStaticContainer.class))
|
||||
.withMessage("Container field 'container' must be static");
|
||||
}
|
||||
|
||||
@Test
|
||||
void importWhenHasContainerDefinitionsWithDynamicPropertySource() {
|
||||
this.applicationContext = new AnnotationConfigApplicationContext(
|
||||
ContainerDefinitionsWithDynamicPropertySource.class);
|
||||
assertThat(this.applicationContext.getEnvironment().containsProperty("container.port")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void importWhenHasNonStaticDynamicPropertySourceMethod() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> this.applicationContext = new AnnotationConfigApplicationContext(
|
||||
NonStaticDynamicPropertySourceMethod.class))
|
||||
.withMessage("@DynamicPropertySource method 'containerProperties' must be static");
|
||||
}
|
||||
|
||||
@Test
|
||||
void importWhenHasBadArgsDynamicPropertySourceMethod() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> this.applicationContext = new AnnotationConfigApplicationContext(
|
||||
BadArgsDynamicPropertySourceMethod.class))
|
||||
.withMessage("@DynamicPropertySource method 'containerProperties' must be static");
|
||||
}
|
||||
|
||||
@ImportTestcontainers
|
||||
static class ImportWithoutValue {
|
||||
|
||||
@ContainerAnnotation
|
||||
static PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageNames.postgresql());
|
||||
|
||||
}
|
||||
|
||||
@ImportTestcontainers(ContainerDefinitions.class)
|
||||
static class ImportWithValue {
|
||||
|
||||
}
|
||||
|
||||
@ImportTestcontainers
|
||||
static class NoContainers {
|
||||
|
||||
}
|
||||
|
||||
@ImportTestcontainers
|
||||
static class NullContainer {
|
||||
|
||||
static PostgreSQLContainer<?> container = null;
|
||||
|
||||
}
|
||||
|
||||
@ImportTestcontainers
|
||||
static class NonStaticContainer {
|
||||
|
||||
PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageNames.postgresql());
|
||||
|
||||
}
|
||||
|
||||
interface ContainerDefinitions {
|
||||
|
||||
@ContainerAnnotation
|
||||
PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageNames.postgresql());
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
static @interface ContainerAnnotation {
|
||||
|
||||
}
|
||||
|
||||
@ImportTestcontainers
|
||||
static class ContainerDefinitionsWithDynamicPropertySource {
|
||||
|
||||
static PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageNames.postgresql());
|
||||
|
||||
@DynamicPropertySource
|
||||
static void containerProperties(DynamicPropertyRegistry registry) {
|
||||
registry.add("container.port", container::getFirstMappedPort);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ImportTestcontainers
|
||||
static class NonStaticDynamicPropertySourceMethod {
|
||||
|
||||
@DynamicPropertySource
|
||||
void containerProperties(DynamicPropertyRegistry registry) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ImportTestcontainers
|
||||
static class BadArgsDynamicPropertySourceMethod {
|
||||
|
||||
@DynamicPropertySource
|
||||
void containerProperties() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,18 +16,29 @@
|
||||
|
||||
package org.springframework.boot.testcontainers.service.connection;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
|
||||
import org.springframework.boot.testcontainers.beans.TestcontainerBeanDefinition;
|
||||
import org.springframework.boot.testcontainers.lifecycle.TestcontainersLifecycleApplicationContextInitializer;
|
||||
import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable;
|
||||
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -78,6 +89,18 @@ class ServiceConnectionAutoConfigurationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenHasTestcontainersBeanDefinition() {
|
||||
try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext()) {
|
||||
applicationContext.register(WithNoExtraAutoConfiguration.class,
|
||||
TestcontainerBeanDefinitionConfiguration.class);
|
||||
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
|
||||
applicationContext.refresh();
|
||||
RedisConnectionDetails connectionDetails = applicationContext.getBean(RedisConnectionDetails.class);
|
||||
assertThat(connectionDetails.getClass().getName()).isEqualTo(REDIS_CONTAINER_CONNECTION_DETAILS);
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ImportAutoConfiguration(ServiceConnectionAutoConfiguration.class)
|
||||
static class WithNoExtraAutoConfiguration {
|
||||
@@ -111,4 +134,42 @@ class ServiceConnectionAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(TestcontainerBeanDefinitionRegistrar.class)
|
||||
static class TestcontainerBeanDefinitionConfiguration {
|
||||
|
||||
}
|
||||
|
||||
static class TestcontainerBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
|
||||
BeanNameGenerator importBeanNameGenerator) {
|
||||
registry.registerBeanDefinition("redisContainer", new TestcontainersRootBeanDefinition());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestcontainersRootBeanDefinition extends RootBeanDefinition implements TestcontainerBeanDefinition {
|
||||
|
||||
private final RedisContainer container = new RedisContainer();
|
||||
|
||||
TestcontainersRootBeanDefinition() {
|
||||
setBeanClass(RedisContainer.class);
|
||||
setInstanceSupplier(() -> this.container);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContainerImageName() {
|
||||
return this.container.getDockerImageName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MergedAnnotations getAnnotations() {
|
||||
MergedAnnotation<ServiceConnection> annotation = MergedAnnotation.of(ServiceConnection.class);
|
||||
return MergedAnnotations.of(Set.of(annotation));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user