Fix @ServiceConnection in native tests
Using `@ServiceConnection` results in the definition of one or more connection details beans. These bean definitions use an instance supplier which is not supported by AOT. This results in a failure during AOT processing. This commit introduces a BeanRegistrationExcludeFilter to exclude from AOT processing the beans created from a `@ServiceConnection`. They are not needed as the registrar will run again in the native image and define the beans at which point the use of an instance supplier is supported again. Fixes gh-35663
This commit is contained in:
@@ -21,6 +21,7 @@ import java.util.Set;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.aot.test.generate.TestGenerationContext;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
@@ -36,11 +37,13 @@ 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.context.aot.ApplicationContextAotGenerator;
|
||||
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.assertj.core.api.Assertions.assertThatNoException;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -101,6 +104,17 @@ class ServiceConnectionAutoConfigurationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceConnectionBeansDoNotCauseAotProcessingToFail() {
|
||||
try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext()) {
|
||||
applicationContext.register(WithNoExtraAutoConfiguration.class, ContainerConfiguration.class);
|
||||
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
|
||||
TestGenerationContext generationContext = new TestGenerationContext();
|
||||
assertThatNoException().isThrownBy(() -> new ApplicationContextAotGenerator()
|
||||
.processAheadOfTime(applicationContext, generationContext));
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ImportAutoConfiguration(ServiceConnectionAutoConfiguration.class)
|
||||
static class WithNoExtraAutoConfiguration {
|
||||
|
||||
Reference in New Issue
Block a user