@DataJPATest with Spring Boot

Unit Test is so important in Software Development. Spring Boot provides @DataJpaTest annotation to make the writing test for JPA applications more simpler.

This tutorial shows you how to apply @DataJpaTest with TestEntityManager.

Related article:
How to use Spring JPA with PostgreSQL | Spring Boot
How to use Spring JPA MySQL | Spring Boot

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.2.RELEASE

II. Overview
1. Data JPA tests

By default, @DataJpaTest will configure an in-memory embedded database, scan for @Entity classes and configure Spring Data JPA repositories. It is also transactional and rollback at the end of each test. If we wanna disable transaction management, we can use:

We can also inject a TestEntityManager bean specifically designed for tests which is an alternative to the JPA EntityManager.

Hence, our Test Class should be similar to:

2. Project Structure

– Class Customer corresponds to entity and table customer, it should be implemented Serializable.
CustomerRepository is an interface extends CrudRepository, will be autowired in WebController for implementing repository methods and custom finder methods.
Dependencies for Spring Boot, JPA and in pom.xml, we also have dependency for hsqldb, this is used for JPA testing purpose.
MyJPAUnitTest is the main Test Class used for testing JPA and annotated with @DataJpaTest.

3. Step to do

– Create Spring Boot project & add Dependencies
– Create DataModel Class
– Create Spring JPA Repository Interface
– Create Class for Data JPA tests
– Run Spring Unit Test & Enjoy Result

III. Practice
1. Create Spring Boot project & add Dependencies

Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields:

Open pom.xml and add Dependencies:

2. Create DataModel Class

Under package model, create class Customer.

Content of Customer.java:

Annotation @Entity indicates that Customer is an Entity and @Table specifies the primary table (name customer) for the annotated @Entity.

@ID specifies the primary key and @GeneratedValue indicates generation strategy for value of primary key.

@Column: mapped column (in the table) for persistent fields (in Java class).

We have 2 constructor methods:
protected constructor will be used by Spring JPA.
public constructor is for creating instances.

3. Create Spring JPA Repository Interface

Under package model, create interface CustomerRepository.
This interface helps us do all CRUD functions for class Customer.

4. Create Class for Data JPA tests

Under src/test/java, create a class named MyJPAUnitTest extending auto-generated testing class. We will test many cases with some methods inside this class.

5. Run Spring Unit Test & Enjoy Result

– Config maven build:
clean install
– Run project with mode JUnit Test
– Check results:

IV. Source Code


Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *