Count and Assert SQL Statements
Description: This application is a sample of counting and asserting SQL statements triggered "behind the scene". Is very useful to count the SQL statements in order to ensure that your code is not generating more SQL statements that you may think (e.g., N+1 can be easily detected by asserting the number of expected statements).
Key points:
- for Maven, in
pom.xml
, add dependencies for DataSource-Proxy library and Vlad Mihalcea's db-util library - create the
ProxyDataSourceBuilder
withcountQuery()
- reset the counter via
SQLStatementCountValidator.reset()
- assert
INSERT
,UPDATE
,DELETE
andSELECT
viaassertInsert/Update/Delete/Select/Count(long expectedNumberOfSql)
Output example (when the number of expected SQLs is not equal with the reality an exception is thrown):