@DataJpaTest 不读取 spring.jpa.* 属性,而 @SpringBootTest 读取

api*_*nes 6 java spring hibernate spring-data-jpa spring-boot

我正在使用 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
Run Code Online (Sandbox Code Playgroud)

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

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ExtendWith(SpringExtension.class)
public class MyTest {
  ...
}
Run Code Online (Sandbox Code Playgroud)

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

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

当将@DataJpaTest注释更改为@SpringBootTest它的工作...

重要的是要注意其余的属性(与我的应用程序特别相关并且没有spring.*前缀的东西正在被正常读取和使用

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

flo*_*all 0

这是一个缩进的问题。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
Run Code Online (Sandbox Code Playgroud)

但如果您使用 logback.xml 进行日志记录配置,您也可以尝试此操作:

<logger name="org.hibernate.stat" level="OFF"/>
Run Code Online (Sandbox Code Playgroud)