测试 Spring Boot Batch 应用程序时如何配置 EntityManagerFactoryBuilder bean?

How to configure EntityManagerFactoryBuilder bean when testing Spring Boot Batch application?

我有一个 Spring 启动批处理应用程序,我正在针对它编写集成测试。但是,当 运行 测试时,我收到以下关于 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 FlatFileItemReader<Foo> reader;

    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();

    public StepExecution getStepExection() {
        StepExecution execution = MetaDataInstanceFactory.createStepExecution();
        return execution;
    }

    @Test
    public void testGoodData() throws Exception {
       //some test code
    }

这是数据库配置 class:

@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;
    }

}

当(集成)测试 Spring 引导应用程序时,您应该这样做。 @SpringApplicationConfiguration 旨在获取您的应用程序 class(带有 @SpringBootApplication 注释的应用程序),因为它将被触发以执行与常规 [=17= 相同的自动配置] 启动应用程序。

您只包含 2 个配置 classes,因此不会进行自动配置。