I recently integrated Envers (Spring Data Envers) into my microservice and I'd like to test it.
I'm able to get my test to spin up a Testcontainer (Postgresql), run the Flyway migrations to create the tables, and do inserts into the tables using a JPA repository that I autowire.
But when I attempt to get the Revisions from the Envers audit table, I get an error stating that the table does not exist.
I suspect this has something to do with how I've configured this test using @DataJpaTest
. @DataJpaTest
disables a lot of functionality that would otherwise be present with @SpringBootTest
, so that only JPA-related code is wired up. I'd like to add Envers to this. It's not clear to me what I need to do to get the Envers code to run and create the _aud
tables.
My test class (Groovy/Spock):
@DataJpaTest
@Testcontainers
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("tc")
class FooRepositoryITSpec extends Specification {
@Autowired
private FooRepository fooRepository
void "test envers functionality"() {
given:
var foo = new Foo()
when:
fooRepository.save(foo)
var revisions = fooRepository.findRevisions(foo.getId()) // ERROR HERE - org.postgresql.util.PSQLException: ERROR: relation "foo_aud" does not exist
then:
revisions.size() == 1
var revision = revisions.collect().get(0)
revision.getRevisionNumber() != null
}
application-tc.yaml
overrides the connection parameters to direct to the ephemeral Postgresql instance running in Docker, which is standard when using Testcontainers:
spring:
datasource:
url: jdbc:tc:postgresql:13:///test_database
username: user
password: password
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver