Skip to content

Commit a88621d

Browse files
authored
Update README.md
1 parent 7b42ff2 commit a88621d

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

README.md

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,53 @@ It consists of just an Hibernate SQL inspector service and a Spring Test Listene
1010

1111
## Example
1212

13-
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:
13+
* 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:
1414

15-
@Test
16-
@Transactional
17-
@AssertSQLStatementCount(deletes = 1, inserts = 2)
18-
void remove_one_entity_and_create_two() {
19-
blogPostRepository.deleteById(1L);
20-
21-
BlogPost post_2 = new BlogPost("Post title 2");
22-
blogPostRepository.save(post_2);
23-
24-
BlogPost post_3 = new BlogPost("Post title 3");
25-
blogPostRepository.save(post_3);
26-
}
27-
28-
If the actual count is different, an exception is thrown:
29-
30-
com.lemick.assertions.HibernateStatementCountException:
31-
Expected 2 INSERT but was 1
32-
Expected 1 DELETE but was 0
3315

34-
You can also use the static methods in your test method, but it's more complex because Hibernate will try to delay the flush of your entities states at the end of your application transaction,
35-
36-
Here we call flush manually:
16+
@Test
17+
@Transactional
18+
@AssertSQLStatementCount(deletes = 1, inserts = 2)
19+
void remove_one_entity_and_create_two() {
20+
blogPostRepository.deleteById(1L);
21+
22+
BlogPost post_2 = new BlogPost("Post title 2");
23+
blogPostRepository.save(post_2);
3724

38-
@Test
39-
@Transactional
40-
void create_two_entities() {
41-
BlogPost post_1 = new BlogPost("Post title 1");
42-
blogPostRepository.save(post_1);
43-
entityManager.flush();
44-
assertInsertStatementCount(1);
25+
BlogPost post_3 = new BlogPost("Post title 3");
26+
blogPostRepository.save(post_3);
27+
}
4528

46-
BlogPost post_2 = new BlogPost("Post title 2");
47-
blogPostRepository.save(post_2);
48-
entityManager.flush();
49-
assertInsertStatementCount(2);
50-
}
29+
If the actual count is different, an exception is thrown:
30+
31+
com.lemick.assertions.HibernateStatementCountException:
32+
Expected 2 INSERT but was 1
33+
Expected 1 DELETE but was 0
34+
35+
* You can also use the static methods in your test method, but it's more complex because Hibernate [will try to delay the flush](https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/chapters/flushing/Flushing.html) of your entities states at the end of your application transaction, here we call flush manually:
36+
37+
@Test
38+
@Transactional
39+
void create_two_entities() {
40+
BlogPost post_1 = new BlogPost("Post title 1");
41+
blogPostRepository.save(post_1);
42+
entityManager.flush();
43+
assertInsertStatementCount(1);
44+
45+
BlogPost post_2 = new BlogPost("Post title 2");
46+
blogPostRepository.save(post_2);
47+
entityManager.flush();
48+
assertInsertStatementCount(2);
49+
}
5150

5251
## How to integrate
53-
- Register the integration with Hibernate, you just need to add this key in your configuration (here for yml):
52+
1. Register the integration with Hibernate, you just need to add this key in your configuration (here for yml):
5453

55-
spring:
56-
jpa:
57-
properties:
58-
hibernate.session_factory.statement_inspector: com.lemick.integration.hibernate.HibernateStatementCountInspector
54+
spring:
55+
jpa:
56+
properties:
57+
hibernate.session_factory.statement_inspector: com.lemick.integration.hibernate.HibernateStatementCountInspector
5958

60-
- Register the Spring TestListener that will launch the SQL inspection if the annotation is present:
59+
2. Register the Spring TestListener that will launch the SQL inspection if the annotation is present:
6160

6261
* By adding the listener on each of your integration test:
6362

0 commit comments

Comments
 (0)