[英]Spring boot cannot create datasource bean from cucumber test context
在基于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
对于clairity,出于特定于应用程序的原因,我不想在运行集成测试时使用内存数据库。
该文件包含:
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启动应用程序时,会读出
src/main/resources/application.properties
并确实正确创建数据源。
我的测试基于黄瓜,并开始使用以下类:
@RunWith(Cucumber.class)
public class AcceptanceTests {
}
使用BaseSteps类上的以下注释开始测试上下文,每个定义黄瓜测试的类继承自
@WebAppConfiguration
@ContextConfiguration(classes = App.class)
spring上下文已成功启动,但无法找到我的application.properties文件和/或使用它。
我找到了解决方案。
我添加了像这样改变了ContextConfiguration:
@ContextConfiguration(classes = App.class, loader = SpringApplicationContextLoader.class)
使用@SpringApplicationConfiguration
而不是ContextConfiguration
。 这是Spring-boot应用程序测试所必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.