Skip to content

@ImportTestcontainers doesn't work with AOT #42891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
* Copyright 2012-2025 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 java.util.function.BiConsumer;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.containers.PostgreSQLContainer;

import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
import org.springframework.boot.testcontainers.lifecycle.TestcontainersLifecycleApplicationContextInitializer;
import org.springframework.boot.testsupport.container.DisabledIfDockerUnavailable;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.aot.ApplicationContextAotGenerator;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.Compiled;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.ClassName;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

import static org.assertj.core.api.Assertions.assertThat;

/**
* AoT Tests for {@link ImportTestcontainers}.
*
* @author Dmytro Nosan
*/
@DisabledIfDockerUnavailable
@CompileWithForkedClassLoader
class ImportTestcontainersAotTests {

private final TestGenerationContext generationContext = new TestGenerationContext();

private final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

@AfterEach
void teardown() {
this.applicationContext.close();
}

@Test
void importTestcontainersImportWithoutValue() {
this.applicationContext.register(ImportWithoutValue.class);
compile((aotApplicationContext, compiled) -> {
PostgreSQLContainer<?> container = aotApplicationContext.getBean(PostgreSQLContainer.class);
assertThat(container).isSameAs(ImportWithoutValue.container);
});
}

@Test
void importTestcontainersImportWithoutValueWithDynamicPropertySource() {
this.applicationContext.register(ImportWithoutValueWithDynamicPropertySource.class);
compile((aotApplicationContext, compiled) -> {
PostgreSQLContainer<?> container = aotApplicationContext.getBean(PostgreSQLContainer.class);
assertThat(container).isSameAs(ImportWithoutValueWithDynamicPropertySource.container);
assertThat(aotApplicationContext.getEnvironment().getProperty("container.port", Integer.class))
.isEqualTo(ImportWithoutValueWithDynamicPropertySource.container.getFirstMappedPort());
});
}

@Test
void importTestcontainersCustomPostgreSQLContainerDefinitions() {
this.applicationContext.register(CustomPostgresqlContainerDefinitions.class);
compile((aotApplicationContext, compiled) -> {
CustomPostgreSQLContainer container = aotApplicationContext.getBean(CustomPostgreSQLContainer.class);
assertThat(container).isSameAs(CustomPostgresqlContainerDefinitions.container);
});
}

@Test
void importTestcontainersImportWithoutValueNotAccessibleContainerAndDynamicPropertySource() {
this.applicationContext.register(ImportWithoutValueNotAccessibleContainerAndDynamicPropertySource.class);
compile((aotApplicationContext, compiled) -> {
MongoDBContainer container = aotApplicationContext.getBean(MongoDBContainer.class);
assertThat(container).isSameAs(ImportWithoutValueNotAccessibleContainerAndDynamicPropertySource.container);
assertThat(aotApplicationContext.getEnvironment().getProperty("mongo.port", Integer.class)).isEqualTo(
ImportWithoutValueNotAccessibleContainerAndDynamicPropertySource.container.getFirstMappedPort());
});
}

@Test
void importTestcontainersWithNotAccessibleContainerAndDynamicPropertySource() {
this.applicationContext.register(ImportWithValueAndDynamicPropertySource.class);
compile((aotApplicationContext, compiled) -> {
PostgreSQLContainer<?> container = aotApplicationContext.getBean(PostgreSQLContainer.class);
assertThat(container).isSameAs(ContainerDefinitionsWithDynamicPropertySource.container);
assertThat(aotApplicationContext.getEnvironment().getProperty("postgres.port", Integer.class))
.isEqualTo(ContainerDefinitionsWithDynamicPropertySource.container.getFirstMappedPort());
});
}

@Test
void importTestcontainersMultipleContainersAndDynamicPropertySources() {
this.applicationContext.register(ImportWithoutValueNotAccessibleContainerAndDynamicPropertySource.class);
this.applicationContext.register(ImportWithValueAndDynamicPropertySource.class);
compile((aotApplicationContext, compiled) -> {
MongoDBContainer mongo = aotApplicationContext.getBean(MongoDBContainer.class);
PostgreSQLContainer<?> postgres = aotApplicationContext.getBean(PostgreSQLContainer.class);
assertThat(mongo).isSameAs(ImportWithoutValueNotAccessibleContainerAndDynamicPropertySource.container);
assertThat(postgres).isSameAs(ContainerDefinitionsWithDynamicPropertySource.container);
ConfigurableEnvironment environment = aotApplicationContext.getEnvironment();
assertThat(environment.getProperty("postgres.port", Integer.class))
.isEqualTo(ContainerDefinitionsWithDynamicPropertySource.container.getFirstMappedPort());
assertThat(environment.getProperty("mongo.port", Integer.class)).isEqualTo(
ImportWithoutValueNotAccessibleContainerAndDynamicPropertySource.container.getFirstMappedPort());
});
}

@SuppressWarnings("unchecked")
private void compile(BiConsumer<GenericApplicationContext, Compiled> result) {
ClassName className = processAheadOfTime();
TestCompiler.forSystem().with(this.generationContext).compile((compiled) -> {
try (GenericApplicationContext aotApplicationContext = new GenericApplicationContext()) {
new TestcontainersLifecycleApplicationContextInitializer().initialize(aotApplicationContext);
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
.getInstance(ApplicationContextInitializer.class, className.toString());
initializer.initialize(aotApplicationContext);
aotApplicationContext.refresh();
result.accept(aotApplicationContext, compiled);
}
});
}

private ClassName processAheadOfTime() {
ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(this.applicationContext,
this.generationContext);
this.generationContext.writeGeneratedContent();
return className;
}

@ImportTestcontainers
static class ImportWithoutValue {

@ContainerAnnotation
static PostgreSQLContainer<?> container = TestImage.container(PostgreSQLContainer.class);

}

@ImportTestcontainers(ContainerDefinitions.class)
static class ImportWithValue {

}

static class ContainerDefinitions {

@ContainerAnnotation
PostgreSQLContainer<?> container = TestImage.container(PostgreSQLContainer.class);

}

private interface ContainerDefinitionsWithDynamicPropertySource {

@ContainerAnnotation
PostgreSQLContainer<?> container = TestImage.container(PostgreSQLContainer.class);

@DynamicPropertySource
static void containerProperties(DynamicPropertyRegistry registry) {
registry.add("postgres.port", container::getFirstMappedPort);
}

}

@Retention(RetentionPolicy.RUNTIME)
@interface ContainerAnnotation {

}

@ImportTestcontainers
static class ImportWithoutValueWithDynamicPropertySource {

static PostgreSQLContainer<?> container = TestImage.container(PostgreSQLContainer.class);

@DynamicPropertySource
static void containerProperties(DynamicPropertyRegistry registry) {
registry.add("container.port", container::getFirstMappedPort);
}

}

@ImportTestcontainers
static class CustomPostgresqlContainerDefinitions {

private static final CustomPostgreSQLContainer container = new CustomPostgreSQLContainer();

}

static class CustomPostgreSQLContainer extends PostgreSQLContainer<CustomPostgreSQLContainer> {

CustomPostgreSQLContainer() {
super("postgres:14");
}

}

@ImportTestcontainers
static class ImportWithoutValueNotAccessibleContainerAndDynamicPropertySource {

private static final MongoDBContainer container = TestImage.container(MongoDBContainer.class);

@DynamicPropertySource
private static void containerProperties(DynamicPropertyRegistry registry) {
registry.add("mongo.port", container::getFirstMappedPort);
}

}

@ImportTestcontainers(ContainerDefinitionsWithDynamicPropertySource.class)
static class ImportWithValueAndDynamicPropertySource {

}

}
Loading