Skip to content

alterioncorp/test-jpa-tracker

Repository files navigation

test-jpa-tracker

License

A small JPA testing utility that automatically cleans up entities created during a test.

The Problem

Integration tests that exercise code which commits its own transaction cannot rely on rolling back the transaction to undo test data. The inserted rows stay in the database and pollute subsequent tests.

The Solution

Wrap your EntityManager with a tracking proxy. The proxy records every entity passed to persist, merge, and remove. When close is called at the end of the test, it deletes all tracked entities in reverse insertion order within its own transaction, respecting foreign key constraints.

Installation

<dependency>
    <groupId>io.github.alterioncorp</groupId>
    <artifactId>test-jpa-tracker</artifactId>
    <version>1.0.0</version>
    <scope>test</scope>
</dependency>

Usage

Wrap the real EntityManager before passing it to the code under test:

EntityManager em = EntityManagerTracker.proxy(realEntityManager);

Use it as you normally would. When close() is called, all tracked entities are deleted automatically:

@Test
void test() {
    EntityManager em = EntityManagerTracker.proxy(realEntityManager);
    try {
        serviceUnderTest.doSomething(em); // may persist and commit internally
    } finally {
        em.close(); // deletes tracked entities
    }
}

Or with try-with-resources, since EntityManager implements AutoCloseable:

@Test
void test() {
    try (EntityManager em = EntityManagerTracker.proxy(realEntityManager)) {
        serviceUnderTest.doSomething(em);
    } // tracked entities deleted here
}

Requirements

  • Java 21+
  • Jakarta Persistence API 3.x (provided by your application or test container)

License

Licensed under the Apache License 2.0.

About

A small JPA testing utility that automatically cleans up entities created during a test.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages