Integration with JUnit

Sniffy comes with a JUnit @Rule for quick integration with test framework. Just add @Rule public final SniffyRule sniffyRule = new SniffyRule(); to your JUnit test class and place appropriate expectations on your test methods like shown below.

package io.sniffy.test.junit.usage;

import io.sniffy.sql.SqlExpectation;
import io.sniffy.test.Count;
import io.sniffy.test.junit.SniffyRule;
import org.junit.Rule;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JUnitUsageTest {

    @Rule
    public final SniffyRule sniffyRule = new SniffyRule(); (1)

    @Test
    @SqlExpectation(count = @Count(1)) (2)
    public void testJUnitIntegration() throws SQLException {
        final Connection connection = DriverManager.getConnection("sniffy:jdbc:h2:mem:", "sa", "sa"); (3)
        connection.createStatement().execute("SELECT 1 FROM DUAL"); (4)
    }

}
  1. - Integrate Sniffy to your test using @Rule annotation and a SniffyRule field.

  2. - Now just add @SqlExpectation annotation to define number of queries allowed for given method.

  3. - Just add sniffy: in front of your JDBC connection URL in order to enable sniffer.

  4. - Do not make any changes in your code - just add the @Rule SniffyRule and put annotations on your test method.