@DataJPATest with Spring Boot
DataJPATest
1
spring boot
78
Testing
31
Male avatar

loveprogramming viết ngày 22/04/2021

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

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


@Autowired
private TestEntityManager entityManager;

Hence, our Test Class should be similar to:


@RunWith(SpringRunner.class)
@DataJpaTest
public class MyJPAUnitTest extends SpringJpaUnitTestApplicationTests {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    CustomerRepository repository;
    
    @Test
    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:

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

@DataJPATest with Spring Boot

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

444 bài viết.
77 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
444 bài viết.
77 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!