Introduction
Changelog
Contact
Guides
Getting Started
Entities
Repositories
Queries & Specifications
Configuration & Pooling
Migrations
Spring Boot
Spigot & Paper Plugins
Recipes
Reference
Databases
Troubleshooting
Advanced
Multi-tenancy
Audit Trail
Testing
TrademarkTrademark
Ctrl k
Search…
Get started
Introduction
Changelog
Contact
Guides
Getting Started
Entities
Repositories
Queries & Specifications
Configuration & Pooling
Migrations
Spring Boot
Spigot & Paper Plugins
Recipes
Reference
Databases
Troubleshooting
Advanced
Multi-tenancy
Audit Trail
Testing
TrademarkTrademark© Aveiro. All rights reserved.
Built with Aveiro
Advanced

Testing

Test repositories against H2 or a real database via Testcontainers with @JEHibernateTest, control reset between tests, and build fixtures.
Updated last month
Audit Trail
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.
testImplementation("de.jexcellence.hibernate:jehibernate-testing:4.2.0")

@JEHibernateTest

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
databaseTestDatabase.H2
H2 in-memory, or a container (POSTGRES, MYSQL, MARIADB, MSSQL)
resetDatabaseReset.TRUNCATE_ALLstate cleanup after each test
ddlAutoschema 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.
@JEHibernateTest(database = TestDatabase.POSTGRES, migration = true, scanPackages = "com.example.domain")
class WarpPostgresIT {
    @Test
    void worksOnPostgres(JEHibernate jeHibernate) { /* ... */ }
}