samedi 10 août 2019

Integration testing with Jooq

How does one write integration tests in JUnit which use JOOQ as data access layer, and which rollback after test completion?

It seems that Jooq provides only very limited transactional management. It offers a method

DSLContext.transaction(TransactionalRunnable transactional)

This method will rollback a transaction if exception is thrown by the passed lambda. But it is not obvious how to make this work with JUnit integration tests. What I really want is an equivalent of @Transactional interface without using Spring.

@Transactional
class UserApiTest
{
  UserApi api;

  @Test
  public void testUpdateUserLastName() {
    User user = getUserByUsername();
    user.setLastName("NewLastName");
    api.updateUser(user);
    assertEquals("NewLastName", user.getLastName());
    // database should be unchanged after test completion because of automatic rollback
  }
}

class UserApiImpl implements UserApi
{
  private final DSLContext db;

  @Override
  public void updateUser(LegacyUser user) {
    UserRecord userRecord = db.newRecord(USER, user);
    db.executeUpdate(userRecord);
  }
}

Any suggestions would be appreciated.

Aucun commentaire:

Enregistrer un commentaire