@DataJpaTest doesn't read spring.jpa.* properties while @SpringBootTest does

Viewed 1164

I'm using Spring Boot 2.0.4.RELEASE, and configured src/test/resources/application.yml to be

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

I have a very simple test:

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ExtendWith(SpringExtension.class)
public class MyTest {
  ...
}

The test ignores the properties (can be easily seen as it prints the hibernate statements). Putting the same properties in a application.properties file is working.

Changing the name to application-test.yml and running on profile test didn't help either.

When changing the @DataJpaTest annotation to @SpringBootTest it's working...

It's important to note that the rest of the properties (things related to my application specifically and are not with spring.* prefix are being read and used normally

I do prefer to use a yaml file (like in /src/main/resources) and rather not load a complete @SpringBootTest just for pure JPA tests... Is there anything else that I can configure for this to work?

2 Answers

It is a problem of indentation. properties has to be moved one level to the left.

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

But you could also try this if you use logback.xml for logging config:

<logger name="org.hibernate.stat" level="OFF"/>

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) - This causes the DataJpaTest to use the same configuration as Spring Boot Main.

Related