【问题标题】:@DataJpaTest doesn't read spring.jpa.* properties while @SpringBootTest does@DataJpaTest 不会读取 spring.jpa.* 属性,而 @SpringBootTest 会
【发布时间】:2019-02-14 06:28:19
【问题描述】:

我正在使用 Spring Boot 2.0.4.RELEASE,并将src/test/resources/application.yml 配置为

spring:
  jpa:
    show-sql: false
    hibernate:
      dialect: org.hibernate.dialect.SQLServer2012Dialect
      ddl-auto: none
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
      properties:
        hibernate:
          generate_statistics: false
          show_sql: false

我有一个非常简单的测试:

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ExtendWith(SpringExtension.class)
public class MyTest {
  ...
}

测试忽略了属性(在打印休眠语句时可以很容易地看到)。将相同的属性放在application.properties 文件中是有效的。

将名称更改为 application-test.yml 并在配置文件测试中运行也无济于事。

@DataJpaTest 注释更改为@SpringBootTest 时,它正在工作...

请务必注意,其余属性(具体与我的应用程序相关且不带 spring.* 前缀的内容正在正常读取和使用

我更喜欢使用 yaml 文件(例如在 /src/main/resources 中),而不是只为纯 JPA 测试加载完整的 @SpringBootTest...还有什么我可以配置的吗?

【问题讨论】:

  • 对我来说它打印 jpa sql 语句而不读取标志,超级烦人!
  • 尝试添加:@TestPropertySource(locations = "classpath:application.yaml")

标签: java spring hibernate spring-boot spring-data-jpa


【解决方案1】:

这是缩进的问题。 properties 必须向左移动一级。

spring:
  jpa:
    show-sql: false
    hibernate:
      dialect: org.hibernate.dialect.SQLServer2012Dialect
      ddl-auto: none
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    properties:
      hibernate:
        generate_statistics: false
        show_sql: false

但如果你使用 logback.xml 来记录配置,你也可以试试这个:

<logger name="org.hibernate.stat" level="OFF"/>

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2021-09-18
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 2021-11-13
    相关资源
    最近更新 更多