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;
 
@Repository
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).

@RunWith(SpringRunner.class)
@DataJpaTest
public class DataRepositoryTest 
{
	//
}

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

@DataJpaTest
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;

@RunWith(SpringRunner.class)
@DataJpaTest
public class EmployeeRepositoryTest 
{
	@Autowired
	EmployeeRepository repository;
	
	@Test
	public void testRepository() 
	{
		EmployeeEntity emp = new EmployeeEntity();
		emp.setFirstName("Lokesh");
		emp.setLastName("Gupta");
		emp.setEmail("howtodoinjava@gmail.com");
		
		repository.save(emp);
		
		Assert.assertNotNull(emp.getId());
	}
}

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?

    Reply
  2. Hey Gaurav,
    Actually Ive a doubt while using @autowired it increase the @ID Ive used
    @Id
    @Column(name=”sno”)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long sno;

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

    Reply
  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?

    Reply

Leave a Comment

HowToDoInJava

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