【发布时间】:2015-08-31 07:13:12
【问题描述】:
在基于 Spring Boot 的应用程序中运行测试时,Spring 无法创建数据源 bean:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
阅读此错误后,我很清楚,我认为 spring 没有读取位于以下位置的 application.properties 文件:
src/test/resources/application.properties
为了清楚起见,出于应用程序特定的原因,我不想在运行集成测试时使用内存数据库。
此文件包含:
spring.datasource.url=jdbc:mysql://127.0.2.1:3306/project-test
spring.datasource.username=foo
spring.datasource.password=bar
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
当使用 bootRun at 启动应用程序时会读出
src/main/resources/application.properties
并且确实正确地创建了数据源。
我的测试是基于 cucumber 并开始使用以下类:
@RunWith(Cucumber.class)
public class AcceptanceTests {
}
测试上下文使用 BaseSteps 类上的以下注释启动,每个定义黄瓜测试的类都继承自该类
@WebAppConfiguration
@ContextConfiguration(classes = App.class)
spring 上下文已成功启动,但无法找到我的 application.properties 文件和/或使用它。
【问题讨论】:
-
您的
application.properties文件在您的应用程序中是如何连接的? -
你试过
jdbc.driverClassName=com.mysql.jdbc.Driver -
@Craig 这就是我想要弄清楚的,spring boot 如何将 src/test/resources/application.properties 文件连接到我的应用程序。
-
@SaviNuclear 我看不出将属性添加到我的配置文件会有什么帮助,因为显然 spring 甚至一开始都没有找到该文件。
标签: java spring spring-boot cucumber