@DataJpaTest is a Spring Boot test slice annotation that starts only the JPA part of your application: entities, Spring Data repositories, the EntityManager and a DataSource. By default it swaps your real database for an embedded one, wraps every test in a transaction and rolls it back afterwards. That makes repository tests fast, isolated and repeatable without loading controllers or services.
Use it to verify derived queries, @Query methods, mappings and constraints. Use @SpringBootTest only when you need the whole application context.
@DataJpaTest is a meta-annotation that combines several pieces of Spring Boot test infrastructure. When a test class carries it, Spring Boot builds a small application context with:
EntityManagerFactory, transaction manager).@SpringBootApplication package.TestEntityManager bean for setting up and inspecting data.DataSource.It does not load @Controller, @Service or @Component beans. If a repository test needs one of them, add it explicitly with @Import.
The example below tests a repository for a Book entity. It uses Spring Boot 3 with the jakarta.persistence package and JUnit 5.
The test persists three rows through TestEntityManager, flushes them so the SQL actually runs, then calls the derived query. When the method ends, Spring rolls the transaction back and the next test starts with an empty table.
Every @DataJpaTest method is @Transactional and rolls back by default. Two consequences are worth knowing:
flush() (or run a query, which flushes automatically in the default flush mode).To keep data after a test, add @Rollback(false) to the method. To run a test without any transaction, use @Transactional(propagation = Propagation.NOT_SUPPORTED). Both are rare and mainly useful when you test transaction boundaries themselves.
An embedded H2 database can behave differently from PostgreSQL or MySQL: different SQL dialect details, JSON types, locking and case rules. When your repository uses native queries or database-specific features, test against the real engine with Testcontainers.
@AutoConfigureTestDatabase(replace = NONE) stops Spring Boot from substituting H2. @ServiceConnection (Spring Boot 3.1 and later) wires the container's URL and credentials into the DataSource without manual property overrides.
| Need | How |
|---|---|
| Add a bean the repository depends on | @Import(MyAuditor.class) on the test class |
| See generated SQL | @DataJpaTest(showSql = true) or spring.jpa.show-sql=true |
| Use only specific properties | @DataJpaTest(properties = "spring.jpa.hibernate.ddl-auto=create-drop") |
| Keep your configured database | @AutoConfigureTestDatabase(replace = Replace.NONE) |
| Load extra auto-configuration | @ImportAutoConfiguration |
Slice tests find your @SpringBootApplication class by walking up the package tree. If that class carries extra annotations such as @EnableJpaAuditing or component scans, they apply to every slice, which can bloat the context. Keep such configuration in separate @Configuration classes instead.
@SpringBootTest loads the full application context and is slower. @DataJpaTest loads only the persistence layer and is meant for repository tests.
No. Services and controllers are not part of the slice. Test controllers with @WebMvcTest and services with plain unit tests or @SpringBootTest.
Hibernate defers writes until flush. Call entityManager.flush() or repository.flush() before asserting, especially for constraint violations.
Yes. Disable the embedded replacement with @AutoConfigureTestDatabase(replace = Replace.NONE) and point the test at a Testcontainers database.
@DataJpaTest gives you a focused, transactional JPA environment with an embedded database and a TestEntityManager. Use it for repository and mapping tests, flush before asserting, and switch to Testcontainers when your queries depend on the behaviour of the production database.