Using Savepoints with JOOQ and JUnit

Viewed 82

I have a bunch of unit tests classes. Each class has about 20 tests. Each class also has a unique database "setup" that establishes the baseline that the tests in the class will assume as their starting point.

The data takes about 5 seconds to stand up, and then the tests take a couple of hundred milliseconds. The problem is I have to stand up the data for every test. I'd like to use savepoints to reduce this overhead. It would reduce the time taken for the tests from minutes to seconds.

We are using JOOQ and Postgres for this.

I was hoping to do something like

public DSLContext dsl;

@BeforeClass
public static void setUp()
{
   //connect to the database
   //open a transaction
   //establish a savepoint
}

@Before
public void restoreSavepoint()
{
    //rollback to the savepoint
    //then the subclass can work of the known-good state

    //Subclasses use this DSLContext as their only knowledge of the database
    dsl = DSL.using(theConnectionThatHadTheSavepointEstablished)

}

When I try in practice, either "theConnection" is in autocommit mode, which immediately closes the transaction, or "theConnection" is not in autocommit mode, which means the tests never see any change in the database.

I feel like I'm missing something obvious. Has anyone made this type of setup work?

0 Answers
Related