【问题标题】:Spring boot cannot create datasource bean from cucumber test contextSpring boot 无法从黄瓜测试上下文创建数据源 bean
【发布时间】: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


【解决方案1】:

我找到了解决方案。

我添加了这样的 ContextConfiguration 更改:

@ContextConfiguration(classes = App.class, loader = SpringApplicationContextLoader.class)

【讨论】:

    【解决方案2】:

    使用@SpringApplicationConfiguration 代替ContextConfiguration。这是 Spring-boot 应用程序测试所必需的。

    【讨论】:

    • 尝试这个时我得到:java.lang.IllegalArgumentException: WebApplicationContext is required
    • @JonasGeiregat 在@SpringApplicationConfiguration 之后添加(classes = SampleDataJpaApplication.class)
    • 我还详细说明了如何配置和启动测试,这可能很重要。
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2015-02-01
    • 2020-12-12
    相关资源
    最近更新 更多