Sniffy - test SQL queries generated by your applications
Protect your application from N+1 and other database issues
Test number of SQL queries
Sniffy provides a convenient API for validating the number of executed database queries, affected database rows or even number of active TCP connections. The main classes you should use are io.sniffy.Sniffy and io.sniffy.Spy.
Spy objects are responsible for recording the executed queries and bytes sent over the wire. Spy stores all the information since the moment it was created. Sniffy class provides convenient factory methods for creating Spy instances
@Rule public SniffyRule sniffyRule = new SniffyRule(); @Rule public ExpectedException thrown = ExpectedException.none(); @Test @SqlExpectation(count = @Count(1)) public void testExpectedOneQueryGotOne() throws SQLException { DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa").createStatement().execute("SELECT 1 FROM DUAL"); // <4> } @Test @SqlExpectation(count = @Count(max = 1), query = SqlStatement.SELECT) public void testExpectedNotMoreThanOneSelectGotTwo() throws SQLException { try (Statement statement = DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa").createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.execute("SELECT 2 FROM DUAL"); } thrown.expect(WrongNumberOfQueriesError.class); }