Integration Testing for JPA Repository in Spring Boot
Why Intergation Test and Not Unit Test ?
Unit Tests : Not ideal for testing Spring Data repositories because they require mocking many JPA components, which can disrupt the repository’s functionality.
Integration Tests : Better suited for testing repositories. These tests check if queries retrieve the correct data from the database and ensure that operations like INSERT, UPDATE and DELETE work as expected.
@DataJpaTest
Annotation
Used to test repositories.
Automatically configures Spring Boot settings to mimic real application behavior.
Scans
@Entity
classes and configures Spring Data JPA repositories.Logs database queries for clarity.
TestEntityManager
Used to interact with the database directly in tests.
Helps populate tst data or veify results.
Example
the github link : Go to Github Repository