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



我正在使用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......我还可以配置其他什么才能使其工作吗?

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE( - 这会导致 DataJpaTest 使用与 Spring Boot Main 相同的配置。

这是一个缩进问题。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"/>

最新更新