As per the DataJpaTest documentation tests annotated with @DataJpaTest
will by default use embedded in-memory database(h2)
By default, tests annotated with @DataJpaTest are transactional and roll back at the end of each test. 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.
But in my case it is not happening, Test class annotated with @DataJpaTest
is loading the postgres dialect instead of using h2 dialect.
Below is the property file from 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.
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. If this property is removed then its loading the h2 dialect for test cases and postgres dialect of main application.
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.
Also there is one more confusion around database-platform
property, as per this documentation
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.