Springboot 3.2.0 未在 JUnit 代码中加载应用程序上下文
回答 2
浏览 1613
2023-12-11
我有这个 JUnit 代码,它在 Springboot 3.1 中工作没有问题。 但是当我升级到Springboot 3.2.0时,它失败了,它说无法加载应用程序上下文。 有人可以帮助我为什么会发生这种情况吗?
下面是代码:
@ExtendWith(SpringExtension.class)
@DataJpaTest
@Import(TestCreateService.class)
public class CreateServiceTest{
@Autowired private TestCreateService testCreateService;
@Autowired private TestRepository testRepository;
...
@Test
public void bulkGetOrCreateTest() throws InterruptedException {
...
assertThat(executorService.awaitTermination(60, TimeUnit.SECONDS)).isTrue();
assertThat(testRepository.count()).isEqualTo(1);
}
}
}
错误:
[ERROR] UploadTestRepositoryTest.find_with_inner_data » IllegalState ApplicationContext failure threshold (1) exceeded: skipping repeated attempt to load context for [MergedContextConfiguration@589d106c testClass = co.domain.system.uploadfile.UploadTestRepositoryTest, locations = [], classes = [co.test.TestApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true"], contextCustomizers = [[ImportsContextCustomizer@23488e7a key = [org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,
Springboot 3.2.0 有什么变化吗? 为存储库编写JUNIT测试的方式是否改变了?
这是我执行一个简单程序时出现的错误:
java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@58a63629 testClass = com.example.helloworld.repository.UserRepositoryTest, locations = [], classes = [com.example.helloworld.HelloWorldApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true"], contextCustomizers = [[ImportsContextCustomizer@7de843ef 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@302c971f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@35841320, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@71075444, 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@884bb438, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@4d15107f, org.springframework.boot.test.context.SpringBootTestAnnotation@cd76d185], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:180)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:130)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:141)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:97)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:247)
at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:163)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:310)
at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735)
at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testEntityManager' defined in class path resource [org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManagerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'testEntityManager' parameter 0: No qualifying bean of type 'jakarta.persistence.EntityManagerFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:802)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:546)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1166)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:563)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:946)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:616)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:753)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:455)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:323)
at org.springframework.boot.test.context.SpringBootContextLoader.lambda$loadContext$3(SpringBootContextLoader.java:137)
at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58)
at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46)
at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1442)
at org.springframework.boot.test.context.SpringBootContextLoader$ContextLoaderHook.run(SpringBootContextLoader.java:552)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:137)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:108)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:225)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:152)
... 17 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'jakarta.persistence.EntityManagerFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1878)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1404)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1348)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:911)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
... 41 more
迁移到 3.2.0 时,另一个注释也存在类似问题。尝试将这些注释一一删除,以查明产生错误的原因。看起来我会押注
@DataJpaTest
。如果您可以将一个非常简单的类与显示该问题的 pom
隔离开来,也会很有帮助。
- Vincent C. 2023-12-11
我删除了@DataJpaTest,我收到了一个新错误。引起原因:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'co.domain.tell.TestRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} What seems to changed hhhhmmmmm
- jetpack 2023-12-11
你能用一个非常简单的项目重现这个吗?一个简单的 pom + 单元测试就足够了。我需要知道你所有依赖项的版本。
- Vincent C. 2023-12-11
我创建了一个具有存储库单元测试并使用@DataJpaTest 的简单项目。我能够重现这个问题。依赖项是: spring-boot-starter 版本 3.2.0、spring-boot-starter-web 版本 3.2.0、spring-boot-starter-test 版本 3.2.0、jakarta.persistence-api 版本 3.2.0-M1、 spring-data-jpa 版本 3.2.0,lombox 版本 1.18.30...这就是我的所有依赖项
- jetpack 2023-12-12
我想我明白了,错误
Fail to load application context
只是一个幌子。内心深处存在一个错误,导致应用程序上下文加载失败。在我的简单程序中,具有正确的依赖项,即使使用 @DataTestJPA 注释也能正常工作
- jetpack 2023-12-12
2 个回答
#1楼
已采纳
得票数 1
通过向枚举添加 columnDefinition
解决了我的问题。我使用 MySQL 作为数据库。
@Enumerated(value = EnumType.STRING)
@Column(name = "type", length = 16, columnDefinition = "VARCHAR(16)")
var type: EnumType = EnumType.FIRST_VALUE
不幸的是,错误不是很清楚,但这解决了我的问题。一旦我启动应用程序(但不在测试中),就会弹出错误。
Spring Boot 3.2.0的发行说明中是否有相关内容?
- jetpack 2023-12-13
经本人调查,该问题是由于springboot从3.1.5升级到3.2.0时,hibernate-core库升级到6.3.1.Final版本导致的。在这种情况下,这意味着我们需要修复与此相关的所有错误?...还有其他解决方法吗?...
- jetpack 2023-12-14
#2楼
得票数 1
我的问题解决如下:我将所有配置从 src/application.yaml 迁移到 test/application.yaml 中,然后我需要创建一个测试 Bean @Primary ,瞧。