我有一个Spring Boot Batch应用程序,我正在编写集成测试.但是,我在运行测试时遇到以下关于EntityManagerFactoryBuilder bean的错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'entityManagerFactory' defined in com.example.DatabaseConfig: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder]: :
No qualifying bean of type [org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
我的理解是Spring Boot在应用程序启动时提供了EntityManagerFactoryBuilder bean.如何在运行测试时提供EntityManagerFactoryBuilder?
这是我的测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {DatabaseConfig.class,BatchConfiguration.class})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,StepScopeTestExecutionListener.class })
public class StepScopeTestExecutionListenerIntegrationTests {
@Autowired
private FlatFileItemReaderMetaDataInstanceFactory.createStepExecution();
return execution;
}
@Test
public void testGoodData() throws Exception {
//some test code
}
这是DatabaseConfig类:
@Configuration
@EnableJpaRepositories(basePackages={"com.example.repository"},entityManagerFactoryRef="testEntityManagerFactory",transactionManagerRef = "testTransactionManager")
public class DatabaseConfig {
@Bean
public LocalContainerEntityManagerfactorybean testEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(dataSource())
.packages("com.example.domain")
.persistenceUnit("testLoad")
.build();
}
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public PlatformTransactionManager testTransactionManager(EntityManagerFactory testEntityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(testEntityManagerFactory);
return transactionManager;
}
}
最佳答案
原文链接:https://www.f2er.com/spring/432258.html