Spting Boot Testing
September 11, 2021
M04 Q44 When do you want use @DataJpaTest for? What does it auto-configure?
You want to use @DataJpaTest annotation whenever writing an Integration Test for JPA related components of your application like Entities or Repositories.
@DataJpaTest annotation configures:
- In-memory embedded database – behavior can be disabled with
@AutoConfigureTestDatabase(replace = Replace.NONE) - Scans and configures
@Entitybeans - Scans and configures Spring Data Repositories
- Configures
TestEntityManager - Does not load other components like
@Component,@Service,@Controlleretc.
Every @DataJpaTest is transactional by default, after each test transaction is rolled back. You can use @Transactional annotation to customize this behavior.
When using @DataJpaTest you can access TestEntityManager, which contains subset of EntityManager methods that are useful for testing.