我正在使用 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 {
...
}
测试忽略属性(可以很容易地看到它打印 hibernate 语句)。将相同的属性放入
application.properties
文件正在运行。将名称更改为
application-test.yml
并且在配置文件测试上运行也无济于事。当更改
@DataJpaTest
注释到 @SpringBootTest
它的工作...需要注意的是其余的属性(与我的应用程序特别相关且不带有
spring.*
前缀的内容正在正常读取和使用 我更喜欢使用 yaml 文件(如在
/src/main/resources
中)而不是加载完整的 @SpringBootTest
仅用于纯 JPA 测试......还有什么我可以配置的吗?
最佳答案
这是缩进的问题。 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"/>
关于java - @DataJpaTest 不读取 spring.jpa.* 属性,而 @SpringBootTest 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52247312/