@DataJPA Test with Spring Boot
Tags: springboot, datajpa, testing
Published: 2021-04-22 11:59:58

https://grokonez.com/testing/datajpatest-with-spring-boot

@DataJPATest with Spring Boot

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

This tutorial shows you how to apply <strong>@DataJpaTest</strong> with <strong>TestEntityManager</strong>.

Related article:
- <a href="https://grokonez.com/spring-framework/spring-boot/use-spring-jpa-mysql-spring-boot" target="_blank" rel="noopener noreferrer">How to use Spring JPA MySQL | Spring Boot</a>
- <a href="https://grokonez.com/tool/maven/unittest-maven-skip-test-springboot">UnitTest – Maven skip test with SpringBoot</a>
- <a href="https://grokonez.com/testing/unit-test-for-spring-mvc-controller-with-spring-boot">Spring Boot Unit Test for Spring MVC Controller</a>

<!--more-->
<div id="tuttoc" class="jsa-toc"></div>

<div id="tutcontents">
<h2>I. Technologies</h2>
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.2.RELEASE
<h2>II. Overview</h2>
<h3>1. Data JPA tests</h3>
By default, <strong>@DataJpaTest</strong> will configure an in-memory embedded database, scan for <strong>@Entity</strong> 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:
<pre class="lang:java">
@Transactional(propagation = Propagation.NOT_SUPPORTED)
</pre>
We can also inject a <code>TestEntityManager</code> bean specifically designed for tests which is an alternative to the JPA <code>EntityManager</code>.
<pre><code class="language-java">
@Autowired
private TestEntityManager entityManager;
</code></pre>
Hence, our Test Class should be similar to:
<pre><code class="language-java">
@RunWith(SpringRunner.class)
@DataJpaTest
public class MyJPAUnitTest extends SpringJpaUnitTestApplicationTests {

	@Autowired
	private TestEntityManager entityManager;

	@Autowired
	CustomerRepository repository;
	
	@Test
	public void testExample() {...}
}</code></pre>
<h3>2. Project Structure</h3>
<img src="https://grokonez.com/wp-content/uploads/2017/02/datajpatest-struct.png" alt="" width="700" height="296" class="alignnone size-full wp-image-2756" />
- Class <strong>Customer</strong> corresponds to entity and table <strong>customer</strong>, it should be implemented <strong>Serializable</strong>.
- <strong>CustomerRepository</strong> is an interface extends <strong>CrudRepository</strong>, will be autowired in <strong>WebController</strong> for implementing repository methods and custom finder methods.
- <strong>Dependencies</strong> for <strong>Spring Boot</strong>, <strong>JPA</strong> and in <strong>pom.xml</strong>, we also have dependency for <strong>hsqldb</strong>, this is used for JPA testing purpose.
- <strong>MyJPAUnitTest</strong> is the main Test Class used for testing JPA and annotated with <strong>@DataJpaTest</strong>.
<h3>3. Step to do</h3>
- Create Spring Boot project &amp; add Dependencies
- Create DataModel Class
- Create Spring JPA Repository Interface
- Create Class for Data JPA tests
- Run Spring Unit Test &amp; Enjoy Result
<h2>III. Practice</h2>
<h3>1. Create Spring Boot project &amp; add Dependencies</h3>

More at:

https://grokonez.com/testing/datajpatest-with-spring-boot

@DataJPATest with Spring Boot