The jehibernate-testing module (test scope) boots JEHibernate for you in a JUnit 5 test — against in-memory H2 or a real database in a Testcontainers container — and resets state between tests.
Annotate the test class; the extension starts the database and injects JEHibernate and EntityManagerFactory as test parameters.
@JEHibernateTest(scanPackages = "com.example.domain")
class WarpRepositoryTest {
@Test
void createsAndReads(JEHibernate jeHibernate) {
var repo = jeHibernate.repositories().get(WarpRepository.class);
var saved = repo.create(new Warp("spawn"));
assertThat(repo.findById(saved.getId())).isPresent();
}
}
Attributes and their defaults:
Attribute
Default
Meaning
database
TestDatabase.H2
H2 in-memory, or a container (POSTGRES, MYSQL, MARIADB, MSSQL)
reset
DatabaseReset.TRUNCATE_ALL
state cleanup after each test
ddlAuto
schema handling for the test database
run migrations before tests
entity/repository packages to scan
create-drop
migration
false
scanPackages
{}
Testing against a real database
Point database at a container type. The extension starts it with Testcontainers.
Container-backed tests skip (they do not fail) when Docker is unavailable, so the same suite runs on a laptop without Docker and in CI with it.
Reset strategies
DatabaseReset controls cleanup after each method: TRUNCATE_ALL (FK-aware truncation, the default), DROP_CREATE, ROLLBACK_TX, or NONE. Pick the fastest one that keeps your tests independent.
Fixtures
Build reusable test data with Fixtures and its builder, so each test starts from a known state without repeating setup.
Two kinds of test
Use @JEHibernateTest for library-level tests against H2 or containers. In a Spring Boot app, prefer @JEHibernateRepositoryTest (see Spring Boot) for a fast, Docker-free context slice.