Integration with Test NG
Sniffy comes with a Test NG listener
for quick integration with test framework.
Just add @Listeners(SniffyTestNgListener.class)
to your TestNG test class and place appropriate expectations on your test methods like shown below.
package io.sniffy.test.testng.usage;
import io.sniffy.socket.DisableSockets;
import io.sniffy.sql.SqlExpectation;
import io.sniffy.test.Count;
import io.sniffy.test.testng.SniffyTestNgListener;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
@Listeners(SniffyTestNgListener.class) // (1)
public class UsageTestNg {
@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)
}
@Test
@DisableSockets // (5)
public void testDisableSockets() throws IOException {
try {
new Socket("google.com", 22); // (6)
fail("Sniffy should have thrown ConnectException");
} catch (ConnectException e) {
assertNotNull(e);
}
}
}
-
- Integrate
Sniffy
to your test using@Listeners(SniffyTestNgListener.class)
. -
- Now just add
@SqlExpectation
annotation to define number of queries allowed for given method. -
- Just add
sniffy:
in front of your JDBC connection URL in order to enable sniffer. -
- Do not make any changes in your code - just add the
@Listeners(SniffyTestNgListener.class)
and put annotations on your test method. -
- Add
@DisableSockets
annotation on your test method or test class and any attempt to open a network connection will fail -
- All socket operations executed within test method annotated with
@DisableSockets
will throw ajava.net.ConnectException