In this blog we will look at how to get started with testing JPA entities and Spring Data Repository in a Spring Boot based application. We will be using JUnit for the same.
I have observed that a good number of projects do not write any tests for JPA entities or the repository layers which make use of the entities to perform CRUD operations. Writing tests for JPA entities and Spring data repositories can be really effective in checking if all the entities are mapped correctly and ensuring that the repository methods implemented by Spring Data along with the custom methods that you write are behaving in the right way. After all , most applications always talk to a database and if your data is not being handled properly, what is the point of having a great user interface or a well designed business layer ?
Since Spring Boot 1.4, testing these layers has become quite easy and more focused. Let us consider a simple One-Many relation between 2 entities, SocialMediaSite and a User.
A SocialMediaSite can have many users which is mapped using the @OneToMany JPA annotation.
SocialMediaSite.java
User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class User { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
private String firstName; | |
private String lastName; | |
@Convert(converter=EmailAddressConverter.class) | |
private EmailAddress email; | |
@ManyToOne | |
private SocialMediaSite socialMediaSite; | |
//Other details. | |
} |
Notice that EmailAddress is a value object.
SocialMediaRepository.java – This is a Spring Data Repository interface( a proxy instance is created via Spring to back this interface)
SocialMediaSiteEntityTest.java – Test the JPA entities.
The setUp method annotated with the @Before annotation above initializes some mock data that we can use for the tests.
The key takeaways from this class:
- @RunWith(SpringRunner .class) – This brings together JUnit and the Spring test module. The SpringRunner class extends SpringJUnit4ClassRunner, so it is pretty much the same that was used earlier. Shorter class names are always pleasing to the eye.
- @DataJpaTest – This is the most important annotation for testing JPA entities in a Spring Boot application. It spawns an in-memory data base and runs our tests against it. Along with this, the JPA entities are scanned, Transactions, Hibernate and Spring Data are also configured.
- TestEntityManager – The @DataJpaTest also configures a TestEntityManager, this is an alternative to the EntityManager. It actually makes use of the EntityManager but has a nice set of methods like persistAndGetId, persistAndFlush etc.
- AssertJ – The code above uses the AssertJ library to perform all the assertions, this is a nice way to get all the assertions done very fluently ! This is pulled in by the spring-boot-starter-test dependency.
- Junit – This is also pulled in by the spring-boot-starter-test dependency
On similar lines, the tests for the Repository class can also be written as shown below.
SocialMediaSiteRepositoryTest .java
Conclusion
As you can see, testing JPA entities and the repository layer in a Spring Boot based application is quite simple. We don’t need configuration for the entire application(all layers) to test the database related functionality. Using the @DataJpaTest in Spring boot helps us in configuring and testing just the JPA section in the code.
Writing good tests for the JPA/Hibernate entities and the repository layer against an embedded database can be extremely useful in the long run. Any changes in the database schema or in the entity mapping which might lead to issues at run time can be caught immediately. In addition one can also see the SQL queries being executed which can be extremely useful.
You can find the code on github.
Note: In case you are are interested in testing JPA entities using Spring Boot 2, JUnit 5 and Java 14 , read my post here .
One thought on “Testing JPA entities in a Spring Boot application”