Skip to content

Commit 7b267fe

Browse files
authored
Update README.md
1 parent 669b656 commit 7b267fe

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Hibernate SQL query count assertions for Spring
22

3-
Hibernate is a powerful ORM, but you need to have control over the executed SQL queries to avoid huge performance problems (N+1 select, silent updates, batch not working, etc...)
3+
Hibernate is a powerful ORM, but you need to have control over the executed SQL queries to avoid huge performance problems (N+1 selects, silent updates, batch insert not working, etc...)
44

5-
You can enable SQL query logging, this is a great help in dev, but this tool helps you to count the executed queries by Hibernate in your integrations test.
5+
You can enable SQL query logging, this is a great help in dev, but this tool helps you to count the executed queries by Hibernate in your integration tests.
66

7-
It consists of just an Hibernate SQL inspector service and a Spring Test Listener that reads it (so no proxy around the Datasource)
7+
It consists of just an Hibernate SQL inspector service and a Spring Test Listener that enable it (so no proxy around the Datasource)
88

99
## Example
1010

11+
You just have to add the @AssertSQLStatementCount annotation to your test and it will verify the SQL statements count at the end of the test:
12+
1113
@Test
1214
@Transactional
1315
@AssertSQLStatementCount(deletes = 1, inserts = 2)
@@ -21,25 +23,31 @@ It consists of just an Hibernate SQL inspector service and a Spring Test Listene
2123
blogPostRepository.save(post_3);
2224
}
2325

26+
If the actual count is different, an exception is thrown:
27+
28+
com.lemick.assertions.HibernateStatementCountException:
29+
Expected 2 INSERT but was 1
30+
Expected 1 DELETE but was 0
31+
2432
## How to integrate
25-
- Register the SQL inspector service in the registry, you just need to add this key in your configuration (here for yml):
33+
- Register the integration with Hibernate, you just need to add this key in your configuration (here for yml):
2634

2735
spring:
2836
jpa:
2937
properties:
3038
hibernate.session_factory.statement_inspector: com.lemick.integration.hibernate.HibernateStatementCountInspector
3139

32-
- Register the Spring TestListener that will launch the SQL inspection if the annotation is present
40+
- Register the Spring TestListener that will launch the SQL inspection if the annotation is present:
3341

34-
By adding the listener on each of your integration test:
42+
* By adding the listener on each of your integration test:
3543

36-
@SpringBootTest
37-
@TestExecutionListeners(listeners = HibernateStatementCountTestListener.class, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
38-
class MySpringBootIntegrationTest {
39-
...
40-
}
44+
@SpringBootTest
45+
@TestExecutionListeners(listeners = HibernateStatementCountTestListener.class, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
46+
class MySpringBootIntegrationTest {
47+
...
48+
}
4149

42-
Or globally by adding a META-INF/spring.factories file that contains the listener:
50+
* **OR** by adding a **META-INF/spring.factories** file that contains the definition, that will register the listener for all your tests:
4351

44-
org.springframework.test.context.TestExecutionListener=com.lemick.integration.spring.HibernateStatementCountTestListener
52+
org.springframework.test.context.TestExecutionListener=com.lemick.integration.spring.HibernateStatementCountTestListener
4553

0 commit comments

Comments
 (0)