当 GitHub 运行我的管道时,我收到
IllegalState Failed to load ApplicationContext for for [WebMergedContextConfiguration@74764622 testClass =
错误。
但是,当我在 Intellij 中使用 Maven 运行测试时,测试通过了,这是管道作业中的问题。
这是 CI 管道:
name: CI workflow
on:
push:
branches:
- main
jobs:
build-deploy:
name: Continuous integration with Maven
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup JDK 17
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 17
- name: Unit tests
run: mvn -B test --file pom.xml
- name: Build the application
run: mvn -B package --file pom.xml
我正在使用 Junit5 和 TestContainers 进行集成测试。
这些是导致管道错误的类:
@Testcontainers
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class ClassroomRepositoryTest {
@Container
static MySQLContainer<?> mySQLContainer = new MySQLContainer<>("mysql:8.0.35");
@Autowired
ClassroomRepository classroomRepository;
@BeforeEach
public void setup() {
Classroom classroom = Classroom.builder()
.id(1L)
.name("math101")
.subject("mathematics")
.code("random")
.build();
classroomRepository.save(classroom);
}
@Test
public void testConnection() {
assertTrue(mySQLContainer.isCreated());
assertTrue(mySQLContainer.isRunning());
}
@Test
public void testFindByCode() {
Optional<Classroom> classroomOptional = classroomRepository.findClassroomByCode("random");
assertTrue(classroomOptional.isPresent());
}
@Test
public void testFindByCodeWithInvalidCode() {
Optional<Classroom> classroomOptional = classroomRepository.findClassroomByCode("wrong-code");
assertFalse(classroomOptional.isPresent());
}
}
@Testcontainers
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class UserRepositoryTest {
@Container
private static MySQLContainer<?> mySQLContainer = new MySQLContainer<>("mysql:8.0.35");
@Autowired
private UserRepository userRepository;
@DynamicPropertySource
static void dynamicProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", mySQLContainer::getJdbcUrl);
registry.add("spring.datasource.username", mySQLContainer::getUsername);
registry.add("spring.datasource.password", mySQLContainer::getPassword);
}
@BeforeEach
public void setup() {
User user = User.builder()
.id(1L)
.firstname("John")
.lastname("Doe")
.username("john doe")
.email("[email protected]")
.password("1234")
.dateOfBirth(LocalDate.of(1980, 9, 9))
.build();
userRepository.save(user);
}
@Test
public void testConnection() {
assertTrue(mySQLContainer.isCreated());
assertTrue(mySQLContainer.isRunning());
}
@Test
public void testFindByEmail() {
Optional<User> userOptional = userRepository.findByEmail("[email protected]");
assertTrue(userOptional.isPresent());
}
@Test
public void testFindByEmailWithInvalidEmail() {
Optional<User> userOptional = userRepository.findByEmail("[email protected]");
assertFalse(userOptional.isPresent());
}
}
我不知道为什么错误只出现在这些存储库测试类上,其他集成测试运行良好,没有错误。
这是来自 github 的堆栈跟踪:
Error: Errors:
Error: ClassBuddyApplicationTests.contextLoads » IllegalState Failed to load ApplicationContext for [WebMergedContextConfiguration@74764622 testClass = com.demo.ClassBuddy.ClassBuddyApplicationTests, locations = [], classes = [com.demo.ClassBuddy.ClassBuddyApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5b67bb7e, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3d246ea3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@63611043, 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@17d88132, org.springframework.boot.test.context.SpringBootTestAnnotation@dbebc8df], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
Error: ClassroomRepositoryTest.simpleTest » IllegalState ApplicationContext failure threshold (1) exceeded: skipping repeated attempt to load context for [MergedContextConfiguration@35c9e3b9 testClass = com.demo.ClassBuddy.repository.ClassroomRepositoryTest, locations = [], classes = [com.demo.ClassBuddy.ClassBuddyApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true"], contextCustomizers = [[ImportsContextCustomizer@11c27ace key = [org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration, org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcClientAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5b67bb7e, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3d246ea3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@3ae14048, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@34be3d80, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@a7ab0a43, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@17d88132, org.springframework.boot.test.context.SpringBootTestAnnotation@ef828610], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
Error: UserRepositoryTest.simpleTest » IllegalState Failed to load ApplicationContext for [MergedContextConfiguration@77aabf26 testClass = com.demo.ClassBuddy.repository.UserRepositoryTest, locations = [], classes = [com.demo.ClassBuddy.ClassBuddyApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true"], contextCustomizers = [[ImportsContextCustomizer@292be875 key = [org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration, org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcClientAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5b67bb7e, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3d246ea3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@3ae14048, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@34be3d80, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@a7ab0a43, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@17d88132, org.springframework.boot.test.context.SpringBootTestAnnotation@ef828610], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
还有这个:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.1.2:test (default-test) on project ClassBuddy:
Error:
Error: Please refer to /home/runner/work/class-buddy/class-buddy/target/surefire-reports for the individual test results.
Error: Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
Error: -> [Help 1]
Error:
Error: To see the full stack trace of the errors, re-run Maven with the -e switch.
Error: Re-run Maven using the -X switch to enable full debug logging.
Error:
Error: For more information about the errors and possible solutions, please read the following articles:
Error: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Error: Process completed with exit code 1.
将java版本升级到21并将spring boot版本升级到3.3.3后,我也遇到了同样的问题。我检查了万无一失的报告。从报告中,我了解到我的 pom.xml 文件中存在一些依赖项的不兼容版本。
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.9.11</version>
</dependency>
我删除了上述依赖项的版本标签,它解决了我的问题。