在Spring中运行单一测试抛出NoSuchBeanDefinitionException?

时间:2015-05-13 18:26:27

标签: java mysql spring jpa spring-boot

人们,我有我的多模块项目,其组成如下:

example-backend-development
--> example-backend-development-domain
--> example-backend-development-service

我正在我的持久性属性中配置所有数据库配置,该属性位于域模块的resources文件夹中。该文件包含:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sarasa_db
spring.datasource.username=root
spring.datasource.password=myPassword

spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true

所以当我从我的服务启动时我没有问题,因为Spring Boot会使用这些数据创建一个DataSource bean。 当我想在Domain模块中创建一个单一测试时,问题就来了。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DAOConfiguration.class)
public class ClientDAOTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClientDAOTest.class);

    @Autowired
    private ClientDAO dao;

    @Test
    public void testFindClient() {
         LOGGER.info("Starting find all clients test");
         Page<Client> clients = this.dao.findAll(new PageRequest(0, 30, null));
       ...
    }
}

DAOConfiguration从PersisteConfiguration导入bean:

@Configuration
@PropertySource("classpath:persistence.properties")
@EnableTransactionManagement
public class PersistenceConfiguration {

    @Autowired
    private DataSource dataSource;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
        entityManager.setDataSource(this.dataSource);
        entityManager.setPackagesToScan(new String[] {"com.example.movies.domain"});
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManager.setJpaVendorAdapter(vendorAdapter);
        return entityManager;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

然后,当我尝试运行testFindAllClient时,我有这个例外:

...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    ... 52 common frames omitted
15:23:33.647 [main] DEBUG o.s.t.c.s.DirtiesContextTestExecutionListener - After test class: context [DefaultTestContext@3e7cddcb testClass = ClientDAOTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3db6fb5d testClass = ClientDAOTest, locations = '{}', classes = '{class com.example.movies.domain.config.DAOConfiguration}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], dirtiesContext [false].

我明白这是因为我没有使用Spring Boot运行它,因此,DAtaSource并没有被创建。你知道我怎么能解决这个问题?提前谢谢!

0 个答案:

没有答案