[英]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.