@DataJPATest with Spring Boot » grokonez

@DataJPATest with Spring Boot » grokonez

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


@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:

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:
@Transactional(propagation = Propagation.NOT_SUPPORTED)
We can also inject a TestEntityManager bean specifically designed for tests which is an alternative to the JPA EntityManager.

private TestEntityManager entityManager;
Hence, our Test Class should be similar to:

public class MyJPAUnitTest extends SpringJpaUnitTestApplicationTests {

private TestEntityManager entityManager;

CustomerRepository repository;

public void testExample() {...}


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

More at:


@DataJPATest with Spring Boot

datajpa springboot testing

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Software Testing 101: Regression Tests, Unit Tests, Integration Tests

How do scientists demonstrate that a drug or vaccine is effective? Putting it to the test. It's your only choice. And, if you're reasonable, you'd never use medications that haven't been thoroughly tested. So, why don't you test software as thoroughly as you should?

Testing Microservices Applications

The shift towards microservices and modular applications makes testing more important and more challenging at the same time. Learn more here.

How to Do API Testing?

API endpoint when you send the get request to that URL it returns the JSON response. In this article, I am going to use postman assertions for all the examples since it is the most popular tool. But this article is not intended only for the postman tool.

Top 10 Automation Testing Tools: 2020 Edition

The demand for delivering quality software faster — or “Quality at Speed” — requires organizations to search for solutions in Agile, continuous integration (CI), and DevOps methodologies. Test automation is an essential part of these aspects.

Where To Learn Test Programming — July 2020 Edition

What do you do when you have lots of free time on your hands? Why not learn test programming strategies and approaches?