The library contains SQL statement validator that helps to verify count of queries generated by Hibernate.
There are two options:
- Using AssertSqlCount class
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserQueryServiceApplication.class)
public class UserControllerTest {
@Before
public void before() {
AssertSqlCount.reset();
}
@Test
public void shouldFindUser() throws Exception {
User user = userRepository.findOne("1");
user.getOrders().size();
AssertSqlCount.assertInsertCount(1);
}
}
- Using matcher
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserQueryServiceApplication.class)
public class UserControllerTest {
@Before
public void before() {
AssertSqlCount.reset();
}
@Test
public void shouldFindUser() throws Exception {
User user = userRepository.findOne("1");
user.getOrders().size();
assertThat(queryCounts(), hasSelectCount(1));
}
}
Add following dependency to pom.xml
<dependency>
<groupId>io.github.yashchenkon</groupId>
<artifactId>assertsqlcount</artifactId>
<version>1.0.0</version>
</dependency>
If you are using Spring Boot, insert the following line to application.properties
spring.jpa.properties.hibernate.session_factory.statement_inspector=io.github.yashchenkon.assertsqlcount.inspector.QueryCountsInspector
Description will be added later.
Please, create pull requests in case of any fixes.