|
3 | 3 |
|
4 | 4 | Hibernate is a powerful ORM, but you need to have control over the executed SQL queries to avoid **huge performance problems** (N+1 selects, batch insert not working...) |
5 | 5 |
|
6 | | -You can enable SQL query logging, this is a great help in dev, but not in production. This tool helps you to count the **executed SQL queries by Hibernate in your integration tests**. |
| 6 | +You can enable SQL query logging, this is a great help in dev, but not in production. This tool helps you to count the **executed SQL queries by Hibernate in your integration tests, it can assert L2C statistics too**. |
7 | 7 |
|
8 | 8 | It consists of just an Hibernate SQL inspector service and a Spring Test Listener that controls it (no proxy around the Datasource). |
9 | 9 |
|
10 | 10 | The assertion will work seamlessly whether you're testing Spring repositories or doing HTTP integration tests. |
11 | 11 |
|
12 | | -## Example |
| 12 | +## Examples |
13 | 13 |
|
14 | | -* You just have to add the @AssertHibernateSQLCount annotation to your test and it will verify the SQL statements (SELECT, UPDATE, INSERT, DELETE) count at the end of the test : |
| 14 | +### Assert SQL statements |
| 15 | + |
| 16 | +You just have to add the @AssertHibernateSQLCount annotation to your test and it will verify the SQL statements (SELECT, UPDATE, INSERT, DELETE) count at the end of the test : |
15 | 17 |
|
16 | 18 |
|
17 | 19 | @Test |
@@ -39,14 +41,35 @@ If the actual count is different, an exception is thrown with the executed state |
39 | 41 | => '/* insert com.lemick.demo.entity.BlogPost */ insert into blog_post (id, title) values (default, ?)' |
40 | 42 | => '/* insert com.lemick.demo.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)' |
41 | 43 | => '/* insert com.lemick.demo.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)' |
| 44 | + |
| 45 | +### Assert L2C statistics |
| 46 | + |
| 47 | +It supports assertions on Hibernate level two cache statistics, for checking that your entities are cached correctly and that will stay forever: |
| 48 | + |
| 49 | + @Test |
| 50 | + @AssertHibernateL2CCount(misses = 1, puts = 1, hits = 1) |
| 51 | + void _create_one_post_and_read_it() { |
| 52 | + doInTransaction(() -> { |
| 53 | + BlogPost post_1 = new BlogPost("Blog post 1"); |
| 54 | + blogPostRepository.save(post_1); |
| 55 | + }); |
| 56 | + |
| 57 | + doInTransaction(() -> { |
| 58 | + blogPostRepository.findById(1L); // 1 MISS + 1 PUT |
| 59 | + }); |
| 60 | + |
| 61 | + doInTransaction(() -> { |
| 62 | + blogPostRepository.findById(1L); // 1 HIT |
| 63 | + }); |
| 64 | + } |
42 | 65 |
|
43 | 66 | ## How to integrate |
44 | 67 | 1. Import the dependency |
45 | 68 |
|
46 | 69 | <dependency> |
47 | 70 | <groupId>com.mickaelb</groupId> |
48 | 71 | <artifactId>hibernate-query-asserts</artifactId> |
49 | | - <version>1.0.0</version> |
| 72 | + <version>2.0.0</version> |
50 | 73 | </dependency> |
51 | 74 |
|
52 | 75 | 2. Register the integration with Hibernate, you just need to add this key in your configuration (here for yml): |
|
0 commit comments