CI kann Testcontainer PostgreSQL nicht starten (ApplicationContext-Fehler) – funktioniert lokalJava

Java-Forum
Anonymous
 CI kann Testcontainer PostgreSQL nicht starten (ApplicationContext-Fehler) – funktioniert lokal

Post by Anonymous »

Code: Select all

  ScheduleRepositoryTest.findById » IllegalState ApplicationContext failure threshold (1) exceeded: skipping repeated attempt to load context for [WebMergedContextConfiguration@b7f12ed testClass = com.infor.eo.db.repositories.ScheduleRepositoryTest, locations = [], classes = [com.infor.eo.db.repositories.RepositoryTestConfig], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [PropertySourceDescriptor[locations=[], ignoreResourceNotFound=false, name=null, propertySourceFactory=null, encoding=null]], propertySourceProperties = ["spring.liquibase.enabled=true", "org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1ef585bb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@24c94e95, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2b6bcf16, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@75f67fe5, org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@59845cb3, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@19740583, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@3b1e32cf], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
[ERROR]   ScheduleRepositoryTest.getExecutors_shouldReturnDistinctExecutorsList » IllegalState Failed to load ApplicationContext for [WebMergedContextConfiguration@b7f12ed testClass = com.infor.eo.db.repositories.ScheduleRepositoryTest, locations = [], classes = [com.infor.eo.db.repositories.RepositoryTestConfig], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [PropertySourceDescriptor[locations=[], ignoreResourceNotFound=false, name=null, propertySourceFactory=null, encoding=null]], propertySourceProperties = ["spring.liquibase.enabled=true", "org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1ef585bb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@24c94e95, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2b6bcf16, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@75f67fe5, org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@59845cb3, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@19740583, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@3b1e32cf], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
[ERROR]   ScheduleRepositoryTest.testFindSchedulesByFilters » IllegalState ApplicationContext failure threshold (1) exceeded:  skipping repeated attempt to load context for [WebMergedContextConfiguration@b7f12ed testClass = com.infor.eo.db.repositories.ScheduleRepositoryTest, locations = [], classes = [com.infor.eo.db.repositories.RepositoryTestConfig], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [PropertySourceDescriptor[locations=[], ignoreResourceNotFound=false, name=null, propertySourceFactory=null, encoding=null]], propertySourceProperties = ["spring.liquibase.enabled=true", "org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1ef585bb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@24c94e95, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2b6bcf16, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@75f67fe5, org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@59845cb3, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@19740583, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@3b1e32cf], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
[ERROR]   ScheduleRepositoryTest.testFindSchedulesWithNullFilters » IllegalState ApplicationContext failure threshold (1) exceeded:  skipping repeated attempt to load context for [WebMergedContextConfiguration@b7f12ed testClass = com.infor.eo.db.repositories.ScheduleRepositoryTest, locations = [], classes = [com.infor.eo.db.repositories.RepositoryTestConfig], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [PropertySourceDescriptor[locations=[], ignoreResourceNotFound=false, name=null, propertySourceFactory=null, encoding=null]], propertySourceProperties = ["spring.liquibase.enabled=true", "org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1ef585bb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@24c94e95, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2b6bcf16, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@75f67fe5, org.springframework.boot.test.autoconfigure.OnFailureConditionReportContextCustomizerFactory$OnFailureConditionReportContextCustomizer@59845cb3, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@19740583, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@0, org.springframework.boot.test.context.SpringBootTestAnnotation@3b1e32cf], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]

This is my confiuration class

Code: Select all

// Source - https://stackoverflow.com/a/79825525
// Posted by Sai Krishna
// Retrieved 2025-11-21, License - CC BY-SA 4.0

package com.infor.eo.db.repositories;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManagerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.transaction.PlatformTransactionManager;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;

@EnableJpaRepositories(basePackages = "com.infor.eo.db.repositories")
@EntityScan("com.infor.eo.db.entities")
@SpringBootConfiguration
@EnableAutoConfiguration
public class RepositoryTestConfig {
@Container
static PostgreSQLContainer  postgresContainer =
new PostgreSQLContainer(DockerImageName.parse("postgres:latest"))
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");

static {
postgresContainer.start();
}

@DynamicPropertySource
public static void properties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgresContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgresContainer::getUsername);
registry.add("spring.datasource.password", postgresContainer::getPassword);
}

@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.infor.eo.db.entities")
.persistenceUnit("default")
.build();
}

@Bean
@ConditionalOnMissingBean(name = "transactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

@Bean
public DataSource dataSource() {
return DataSourceBuilder.create()
.url(postgresContainer.getJdbcUrl())
.username(postgresContainer.getUsername())
.password(postgresContainer.getPassword())
.driverClassName("org.postgresql.Driver")
.build();
}
}

Code: Select all

// Source - https://stackoverflow.com/a/79825525
// Posted by Sai Krishna
// Retrieved 2025-11-21, License - CC BY-SA 4.0

package com.infor.eo.db.repositories;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManagerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.transaction.PlatformTransactionManager;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.utility.DockerImageName;

import javax.sql.DataSource;

@EnableJpaRepositories(basePackages = "com.infor.eo.db.repositories")
@EntityScan("com.infor.eo.db.entities")
@SpringBootConfiguration
@EnableAutoConfiguration
public class RepositoryTestConfig {
@Container
static PostgreSQLContainer  postgresContainer =
new PostgreSQLContainer(DockerImageName.parse("postgres:latest"))
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");

static {
postgresContainer.start();
}

@DynamicPropertySource
public static void properties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgresContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgresContainer::getUsername);
registry.add("spring.datasource.password", postgresContainer::getPassword);
}

@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.infor.eo.db.entities")
.persistenceUnit("default")
.build();
}

@Bean
@ConditionalOnMissingBean(name = "transactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

@Bean
public DataSource dataSource() {
return DataSourceBuilder.create()
.url(postgresContainer.getJdbcUrl())
.username(postgresContainer.getUsername())
.password(postgresContainer.getPassword())
.driverClassName("org.postgresql.Driver")
.build();
}
}
Unsere Repository-Tests (JPA-Repositorys mit Testcontainern + PostgreSQL) bestehen lokal, schlagen jedoch in CI wiederholt mit diesem Symptom fehl:

Code: Select all

IllegalState ApplicationContext failure threshold (1) exceeded: skipping repeated attempt to load context for [WebMergedContextConfiguration...]
Dies ist das ApplicationContext-Symptom – die Hauptursache ist, dass der PostgreSQL-Testcontainer entweder nicht gestartet werden kann oder der Testjob Docker nicht erreichen kann. Ich habe versucht, das Bild an postgres:14-alpine anzuheften und postgres:latest zu entfernen (lokale Tests bestehen jetzt), aber CI schlägt immer noch fehl.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post