[英]Tests with @DataJpaTest are not using embedded in-memory database
As per the DataJpaTest documentation tests annotated with @DataJpaTest
will by default use embedded in-memory database(h2)根据DataJpaTest 文档,使用
@DataJpaTest
注释的测试将默认使用嵌入式内存数据库(h2)
By default, tests annotated with @DataJpaTest are transactional and roll back at the end of each test.
默认情况下,使用 @DataJpaTest 注释的测试是事务性的,并在每个测试结束时回滚。 They also use an embedded in-memory database (replacing any explicit or usually auto-configured DataSource).
他们还使用嵌入式内存数据库(替换任何显式或通常自动配置的数据源)。 The @AutoConfigureTestDatabase annotation can be used to override these settings.
@AutoConfigureTestDatabase 注释可用于覆盖这些设置。
But in my case it is not happening, Test class annotated with @DataJpaTest
is loading the postgres dialect instead of using h2 dialect.但在我的情况下,它没有发生,用@DataJpaTest 注释的测试
@DataJpaTest
正在加载 postgres 方言而不是使用 h2 方言。
Below is the property file from main/resources下面是来自 main/resources 的属性文件
spring:
datasource:
url: jdbc:postgresql://localhost:5432/postgres?currentSchema=test
username: test
password: test
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
Whats happening发生了什么
I found that spring is not picking dialect from application.yml
file from main/resources instead of honouring @DataJpaTest
and using h2 dialect.我发现 spring 没有从 main/resources 的
application.yml
文件中选择方言,而不是尊重@DataJpaTest
并使用 h2 方言。
Solution解决方案
While trying different variations with jpa properties, I found that this issue is happening only when database-platform
property is present in the application properties file.在尝试使用 jpa 属性的不同变体时,我发现仅当应用程序属性文件中存在
database-platform
属性时才会发生此问题。 If this property is removed then its loading the h2 dialect for test cases and postgres dialect of main application.如果此属性被删除,那么它会加载测试用例的 h2 方言和主应用程序的 postgres 方言。
Why?为什么?
This seems to be a bug in the spring codebase, since it is nowhere documented that one needs to remove database-platform
property for @DataJpaTest
to use in-memory database.这似乎是 spring 代码库中的一个错误,因为没有记录表明需要删除
database-platform
属性以使@DataJpaTest
使用内存数据库。
Also there is one more confusion around database-platform
property, as per this documentation根据本文档,关于
database-platform
属性还有一个更混乱的地方
spring.jpa.database-platform
spring.jpa.database-platform
Name of the target database to operate on, auto-detected by default.
要操作的目标数据库的名称,默认自动检测。 Can be alternatively set using the "Database" enum.
也可以使用“数据库”枚举进行设置。
Seems like one more bug in the documentation, as the spring.jpa.database-platform
only accepts dialect class as a value and no other enum value will work.似乎是文档中的另一个错误,因为
spring.jpa.database-platform
仅接受方言 class 作为值,没有其他枚举值将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.