Spring Boot – @DataJpaTest

Learn about Spring Boot @DataJpaTest annotation and how to use it for testing JPA repositories marked with @Repository annotation.

1. @Repository annotation

In spring framework, @Repository is one of the stereotype annotations which enable annotated classes to be discovered and registered with application context.

@Repository annotation is a general-purpose which can be applied over DAO classes as well as DDD-style repositories.

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.howtodoinjava.demo.model.EmployeeEntity;
public interface EmployeeRepository
        extends JpaRepository<EmployeeEntity, Long> 

2. @DataJpaTest annotation

In Spring boot applications, we can use @DataJpaTest annotation that focuses only on JPA components. This annotation will disable full auto-configuration and instead apply only configuration relevant to JPA tests.

By default, tests annotated with @DataJpaTest are transactional and roll back at the end of each test.

By default, it scans for @Entity classes and configures Spring Data JPA repositories annotated with @Repository annotation.

If an embedded database is available on the classpath, it configures one as well.

A nice feature is that these tests may also inject a TestEntityManager bean, which provides an alternative to the standard JPA EntityManager that is specifically designed for tests.

When using JUnit 4, this annotation should be used in combination with @RunWith(SpringRunner.class).

public class DataRepositoryTest 

When using JUnit 5, this annotation already contains the statement @ExtendWith(SpringExtension.class), so no additional runner definition is required.

public class DataRepositoryTest 

3. Spring boot @DataJpaTest example

Given EmployeeRepositoryTest class tests the EmployeeRepository which is of type JpaRepository and is used to store and retrieve EmployeeEntity instances from underlying databases.

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.howtodoinjava.demo.model.EmployeeEntity;
import com.howtodoinjava.demo.repository.EmployeeRepository;

public class EmployeeRepositoryTest 
	EmployeeRepository repository;
	public void testRepository() 
		EmployeeEntity emp = new EmployeeEntity();

Watch the program output in console. You will find that only JPA related configuration has been loaded into context and then testcase execution starts.

Drop me your questions related to Spring Boot @DataJpaTest annotation as discussed above.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

3 thoughts on “Spring Boot – @DataJpaTest”

  1. Hi
    I have a question. We use MySQL. The @DataJpaTest is not rolling back my transaction. can u explain why this might happen?

  2. Hey Gaurav,
    Actually Ive a doubt while using @autowired it increase the @ID Ive used
    private Long sno;

    asserNotBull(emp.getSno()); it increases the sno atuomatically everytime I run it.

  3. hey, I am writing JUnit fro the same JPA repository, and I am stuck in a silly issue, as when I autowire my repository in a test class, it is showing my repository as null, tried all possible way still same issue. could you please suggest on this?


Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.