Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring中运行单一测试不会引发此类BeanDefinitionException异常?_Java_Mysql_Spring_Jpa_Spring Boot - Fatal编程技术网

Java 在Spring中运行单一测试不会引发此类BeanDefinitionException异常?

Java 在Spring中运行单一测试不会引发此类BeanDefinitionException异常?,java,mysql,spring,jpa,spring-boot,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 sprin

各位,我的多模块项目由以下部分组成:

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
所以,当我从我的服务启动时,我没有问题,因为SpringBoot使用这些数据创建了一个数据源bean。 当我想在域模块内创建一个单一测试时,问题就来了

@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));
       ...
    }
}
然后,当我尝试运行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运行它,所以没有创建数据源。你知道我该怎么解决这个问题吗?提前谢谢

我知道如果我手动创建DataSourceBean,问题就会解决,但我更喜欢另一种解决方案。您可以提供更多详细信息、服务中的属性以及服务配置。您当前项目的文件关系图。我认为您没有加载正确的属性文件。您不加载正确的属性文件是什么意思?在PersistenceConfiguration中,我加载persistence.properties文件,在ClientDAOTest中,我加载ServicesConfiguration,它导入PersistenceConfiguration,因此必须调用persistence.properties。我认为所有这些问题都是因为出于某种原因,SpringBootDataJPA没有创建DataSourceBean,而SpringBootStarterWeb创建了DataSourceBean。你认为我说的对吗?不对,spring boot data jpa创建数据源你可以在这里检查我的配置你可以加载你的项目或类似的项目来复制吗?谢谢你的回答。但是我不明白,关于这个文档,application.properties中的那些属性应该以spring开头,你调用的不是spring boot使用的同一个数据源生成器,是吗?当做
...
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].