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

前端之家收集整理的这篇文章主要介绍了如何在测试Spring Boot Batch应用程序时配置EntityManagerFactoryBuilder bean?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个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;
    }

}
最佳答案
当(集成)测试Spring Boot应用程序时,你应该做什么. @SpringApplicationConfiguration旨在获取您的应用程序类(具有@SpringBootApplication批注的应用程序类),因为它将被触发以执行与常规Spring Boot应用程序相同的自动配置.

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

原文链接:https://www.f2er.com/spring/432258.html

猜你在找的Spring相关文章