【问题标题】:spring-boot not using application.properties in testsspring-boot 未在测试中使用 application.properties
【发布时间】:2014-11-30 09:23:36
【问题描述】:

我有一个带有 spring、hibernate 和 flyway 的项目来创建数据库模式。所以我有

spring.jpa.hibernate.ddl-auto: validate

在我的 application.properties 文件中。此配置在正常运行期间有效(在打包可执行 jar 文件并从终端运行之后):

2014-10-06 10:06:17.863  INFO 7519 --- [           main] o.h.tool.hbm2ddl.SchemaValidator         : HHH000229: Running schema validator

但在通过 maven 运行测试时会被忽略。

1804 [main] INFO  o.h.tool.hbm2ddl.SchemaExport - HHH000227: Running hbm2ddl schema export 
1805 [main] DEBUG org.hibernate.SQL - drop table test_entity if exists 
1806 [main] DEBUG org.hibernate.SQL - drop sequence hibernate_sequence 
1807 [main] DEBUG org.hibernate.SQL - create table test_entity (id bigint not null, name    varchar(255), primary key (id)) 
1807 [main] DEBUG org.hibernate.SQL - create sequence hibernate_sequence 
1808 [main] INFO  o.h.tool.hbm2ddl.SchemaExport - HHH000230: Schema export complete 

与官方 flyway-sample 的主要区别似乎在于我不使用 spring-boot 提供的 maven-parent。

完整的项目是here

【问题讨论】:

    标签: hibernate spring-boot flyway


    【解决方案1】:

    您应该定义 ConfigFileApplicationContextInitializer 以将 application.properties 文件包含在您的集成测试中。只需将您的注释更改为:

    @ContextConfiguration(classes = FlywaySpringBootTestApplication.class, initializers = ConfigFileApplicationContextInitializer.class)
    

    我已经向你发送了一个包含这个小改动的拉取请求。

    【讨论】:

      【解决方案2】:

      您的测试没有使用 Spring Boot(它需要使用 @SpringApplicationConfiguration 而不是 @ContextConfiguration,或者声明适当的侦听器)。

      【讨论】:

        猜你喜欢
        • 2019-11-09
        • 1970-01-01
        • 2021-10-25
        • 2018-02-25
        • 2017-07-16
        • 2021-12-25
        • 2016-02-12
        • 1970-01-01
        • 2019-08-20
        相关资源
        最近更新 更多