In this tutorial, you will learn to implement an integration test of the JPA and Hibernate data layer in Spring Boot by using @DataJPATest annotation with in-memory database

@DataJPATest provides the following features

  • Configure the in-memory test database
  • Auto-scan @Entity classes
  • Auto configure Spring Data JPA, Hibernate and Data Source
  • Turn on SQL logging

Project dependencies

Include spring-boot-starter-test and hsqldb into your pom.xml file



spring-boot-starter-test contains some testing support libraries such as JUnit, Spring Test + Spring Boot Test, Mockito, AssertJ, Hamcrest and JsonPath

Define the test class

Run the tests with @RunWith(SpringRunner.class) and @DataJPATest

  • Use @RunWith(SpringRunner.class) class annotation to tell JUnit to run the unit tests in Spring's testing supports
  • Use @DataJPATest class annotation to test JPA and Hibernate layer

Inject dependencies with @Autowired

  • Inject TestEntityManager field
private TestEntityManager testEntityManager;  

TestEntityManager is designed for tests, provides an alternative to the JPA standard EntityManager

  • Inject the JPA Repository you want to test, for example
private ProductRespository productRespository;  

Verify the test result with AssertJ's assertThat(..).

  • Prefer AssertJ's assertThat to verify the test result for more readability, for example
// when
List<Product> products = productRespository.findAll();

// then

Implementation example

@DataJPATest by default will begin a new transaction before and roll back it after running a test method. You should see something like this in the console when running the test

Began transaction (1) for test context [DefaultTestContext@43ee72e6 testClass = ProductRepositoryTest... testMethod = whenFindByName_thenReturnProduct@ProductRepositoryTest...  
Rolled back transaction for test:  [DefaultTestContext@43ee72e6 testClass = ProductRepositoryTest... testMethod = whenFindByName_thenReturnProduct@ProductRepositoryTest...  

Source code