public final class DbSetupTracker extends java.lang.Object
This class allows speeding up test execution, by avoiding re-executing the same sequence of database operations before each test method even if each of these test methods leaves the database as it is (and only performs read-only operations, which is the most frequent case).
Example usage:
// the tracker is static because JUnit uses a separate Test instance for every test method. private static DbSetupTracker dbSetupTracker = new DbSetupTracker(); @Before public void setUp() throws Exception { Operation operation = Operations.sequenceOf( CommonOperations.DELETE_ALL, CommonOperations.INSERT_REFERENCE_DATA, Operations.insertInto("CLIENT") .columns("CLIENT_ID", "FIRST_NAME", "LAST_NAME", "DATE_OF_BIRTH", "COUNTRY_ID") .values(1L, "John", "Doe", "1975-07-19", 1L) .values(2L, "Jack", "Smith", "1969-08-22", 2L) .build()); DbSetup dbSetup = new DbSetup(new DataSourceDestination(dataSource), operation); dbSetupTracker.launchIfNecessary(dbSetup); } @Test public void readOnlyTest1() { dbSetupTracker.skipNextLaunch(); ... } @Test public void readOnlyTest2() { dbSetupTracker.skipNextLaunch(); ... } @Test public void readOnlyTest3() { dbSetupTracker.skipNextLaunch(); ... } @Test public void readWriteTest1() { // No call to dbSetupTracker.skipNextLaunch(); ... }
Constructor and Description |
---|
DbSetupTracker() |
Modifier and Type | Method and Description |
---|---|
void |
launchIfNecessary(DbSetup dbSetup)
Executes the given DbSetup unless all the following conditions are
true :
skipNextLaunch() has been called since the last call to this method;
the given dbSetup is equal to the last DbSetup launched by this method
This method resets the skipNextLaunch flag to false . |
void |
skipNextLaunch()
Marks the current test method as read-only, and thus the need for the next test method to re-execute the same
sequence of database setup operations.
|
java.lang.String |
toString() |
public DbSetupTracker()
public void launchIfNecessary(@Nonnull DbSetup dbSetup)
true
:
skipNextLaunch()
has been called since the last call to this method;dbSetup
is equal to the last DbSetup launched by this methodskipNextLaunch
flag to false
.dbSetup
- the DbSetup to execute (or skip)public void skipNextLaunch()
public java.lang.String toString()
toString
in class java.lang.Object