yannbriancon / spring-hibernate-query-utils

Library giving tools to detect N+1 queries and count the queries generated with Spring and Hibernate
MIT License
135 stars 15 forks source link

Support working with @DataJpaTest #20

Open Anshelen opened 3 years ago

Anshelen commented 3 years ago

For tests with @DataJpaTest HibernateQueryInterceptor is not registered automatically.

I can not load it somehow cause it is package visible only. Also I tried to add spring.jpa.properties.hibernate.session_factory.interceptor=com.yannbriancon.interceptor.HibernateQueryInterceptor, but without effect. The possible workaround is to copy-paste HibernatePropertiesConfig and add @Import({HibernateQueryInterceptor.class, HibernatePropertiesConfig.class}) to test class

eidottermihi commented 3 years ago

I got it working with @DataJpaTest with the following test config (JUnit 5):

@ExtendWith(SpringExtension.class)
@DataJpaTest
@ComponentScan(basePackages = { "com.yannbriancon" }) // hibernateQueryInterceptor
class RepositoryTest {
  @Autowired
  private HibernateQueryInterceptor hibernateQueryInterceptor;

  // .....
}
Anshelen commented 3 years ago

Solution is working. Thanks!