See: Description
Interface | Description |
---|---|
Constants |
Utility interface for storing the Sniffer constants
|
Sniffer.Executable |
Executable interface is similar to
Runnable but it allows throwing Exception
from it's Sniffer.Executable.execute() method |
Spy.Expectation |
Class | Description |
---|---|
MockDriver |
Enable JDBC Sniffer by adding a
sniffer: prefix to your JDBC URL. |
Sniffer |
Sniffer is an entry point for using Sniffy library
See the
package overview
for more information.
|
Spy<C extends Spy<C>> |
Spy holds a number of queries which were executed at some point of time and uses it as a base for further assertions
|
SpyWithValue<V> |
Enum | Description |
---|---|
Query | |
Threads |
Sniffy allows to validate the number of queries generated from different threads
You can use Threads enum to select all threads (ANY), current thread (CURRENT) or
all threads except current (OTHERS)
|
Exception | Description |
---|---|
SpyClosedException |
Error | Description |
---|---|
SniffyAssertionError | |
WrongNumberOfQueriesError | |
WrongNumberOfRowsError |
Annotation Type | Description |
---|---|
Expectation |
Adds an expectation about the number of queries generated by given test method
|
Expectations |
Container for multiple
Expectation annotations |
NoQueriesAllowed |
Alias for
@Expectation(count = @Count(0), threads = Threads.CURRENT) |
@Test
public void testVerifyApi() throws SQLException {
// Just add sniffer: in front of your JDBC connection URL in order to enable sniffer
Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa");
// Spy holds the amount of queries executed till the given amount of time
// It acts as a base for further assertions
Spy spy = Sniffer.spy();
// You do not need to modify your JDBC code
connection.createStatement().execute("SELECT 1 FROM DUAL");
assertEquals(1, spy.executedStatements());
// Sniffer.verifyAtMostOnce() throws an AssertionError if more than one query was executed;
spy.verifyAtMostOnce();
// Sniffer.verifyNever(Threads.OTHERS) throws an AssertionError if at least one query was executed
// by the thread other than then current one
spy.verifyNever(Threads.OTHERS);
}
@Test
public void testFunctionalApi() throws SQLException {
// Just add sniffer: in front of your JDBC connection URL in order to enable sniffer
final Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa");
// Sniffer.execute() method executes the lambda expression and returns an instance of Spy
// which provides methods for validating the number of executed queries in given lambda
Sniffer.execute(() - > connection.createStatement().execute("SELECT 1 FROM DUAL")).verifyAtMostOnce();
}
@Test
public void testResourceApi() throws SQLException {
// Just add sniffer: in front of your JDBC connection URL in order to enable sniffer
final Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa");
// You can use Sniffer in a try-with-resource block using expect methods instead of verify
// When the try-with-resource block is completed, Sniffy will verify all the expectations defined
try ( @SuppressWarnings("unused") Spy s = Sniffer.expectAtMostOnce().expectNever(Threads.OTHERS);
Statement statement = connection.createStatement()) {
statement.execute("SELECT 1 FROM DUAL");
}
}
for the detailed API description
Copyright © 2016. All rights reserved.