@DataJpaTest is a Spring Boot test annotation that starts only the JPA slice of your application: entities, Spring Data repositories, an EntityManager and a DataSource. It does not load controllers, services or the rest of the context, so repository tests start fast and fail only for persistence reasons. By default each test runs in a transaction that is rolled back afterwards, and the real database is replaced with an embedded one.
Use it to verify derived queries, @Query methods, entity mappings and constraints, not business logic.
The annotation is a test slice. Spring Boot applies a limited set of auto-configurations and scans only persistence-related components. In practice you get:
DataSource, a JdbcTemplate and the JPA EntityManagerFactory.TestEntityManager bean, a thin wrapper that makes test setup easier.Beans annotated with @Service, @Component, @Controller or @RestController are not registered. If a repository test needs one of them, import it explicitly with @Import.
Assume a simple entity and repository:
The test uses @DataJpaTest and injects the repository directly:
persistAndFlush writes the row to the database inside the test transaction, so the query in the repository sees it. Because the transaction is rolled back after each test, the second test starts with an empty table.
Note that in Spring Boot 4 the test slices were moved into technology-specific modules and packages, so check the import for DataJpaTest against the Boot version you use. The annotation itself works the same way.
Every test method runs in a transaction that is rolled back at the end. This keeps tests independent without any cleanup code, but it has one side effect: Hibernate may not send SQL until it flushes. A test that saves an entity and queries it through a derived query will flush automatically, yet constraint violations may only appear on flush. Call flush() (or persistAndFlush) when you want the database to check a constraint inside the test.
You can opt out for a single test or class with @Transactional(propagation = Propagation.NOT_SUPPORTED), or keep a result with @Commit. Prefer the default rollback behaviour.
By default @DataJpaTest replaces your configured DataSource with an embedded database such as H2, HSQLDB or Derby, if one is on the classpath. This is fast, but H2 does not behave exactly like PostgreSQL or MySQL, so native queries, JSON columns and locking can pass in tests and fail in production.
To test against the real engine, disable the replacement and start a container with Testcontainers. Since Spring Boot 3.1, @ServiceConnection wires the container into the context without manual properties:
If you use Flyway or Liquibase in production, the same migrations run against the container, which also checks your migration scripts.
Good targets for a @DataJpaTest:
| What to test | Why it belongs here |
|---|---|
Derived queries such as findByEmail | A typo in a property name fails at context start-up |
@Query JPQL and native queries | Syntax and result mapping are verified against a database |
| Unique and not-null constraints | The database rejects bad data, and you assert the exception |
| Cascades and orphan removal | Behaviour depends on the mapping, not on a service |
| Pagination and sorting | Confirms the generated SQL returns the expected slice |
For example, a unique constraint check:
Do not use it for code that coordinates several beans, such as a service that calls a repository and a message broker. Use a plain unit test with mocks for the service logic, or @SpringBootTest for a full integration test. Do not use it for controllers either; @WebMvcTest is the matching slice for the web layer.
If you need a bean outside the slice, add @Import(MyConfig.class) rather than switching to @SpringBootTest, which keeps the test fast.
Yes. Each test method runs in a transaction that is rolled back when the test ends, unless you annotate it with @Commit or change the transaction behaviour.
The slice does not scan @Service or @Component classes. Add them with @Import, or test the service separately with a mock repository.
Add @AutoConfigureTestDatabase(replace = Replace.NONE) and provide the connection through Testcontainers with @ServiceConnection, or through your own test properties.
It lets you persist, flush and clear entities in test setup without going through the repository under test, so you can prepare data independently.
@DataJpaTest gives you a fast, focused test of the persistence layer: entities, repositories and queries, with automatic rollback. Use the embedded database for quick feedback and switch to Testcontainers when your SQL depends on the production engine. Keep service and web logic out of these tests and cover them with unit tests or other slices.