0
0
Spring Bootframework~5 mins

@DataJpaTest for repository testing in Spring Boot

Choose your learning style9 modes available
Introduction

@DataJpaTest helps you test your database repositories easily without starting the whole application. It sets up only what is needed for database tests.

When you want to check if your database queries return correct data.
When you want to test saving, updating, or deleting data in your repository.
When you want to test repository methods without loading the full Spring Boot app.
When you want fast tests focused only on JPA components.
When you want to verify your entity mappings and database interactions.
Syntax
Spring Boot
@DataJpaTest
public class YourRepositoryTest {
    @Autowired
    private YourRepository repository;

    @Test
    public void testMethod() {
        // test code here
    }
}

@DataJpaTest loads only JPA-related components like repositories and configures an in-memory database by default.

You usually use @Autowired to inject the repository you want to test.

Examples
This example tests a custom method findByUsername in the UserRepository.
Spring Boot
@DataJpaTest
public class UserRepositoryTest {
    @Autowired
    private UserRepository userRepository;

    @Test
    public void testFindByUsername() {
        User user = new User("john", "John Doe");
        userRepository.save(user);
        User found = userRepository.findByUsername("john");
        assertEquals("John Doe", found.getFullName());
    }
}
This example disables the default in-memory database to use the real database configured in your app.
Spring Boot
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class ProductRepositoryTest {
    @Autowired
    private ProductRepository productRepository;

    @Test
    public void testSaveProduct() {
        Product product = new Product("Book", 9.99);
        productRepository.save(product);
        assertTrue(productRepository.findById(product.getId()).isPresent());
    }
}
Sample Program

This test saves a book entity and then finds it by title using the repository method. It checks that the saved book is found correctly.

Spring Boot
package com.example.demo;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

@DataJpaTest
public class BookRepositoryTest {

    @Autowired
    private BookRepository bookRepository;

    @Test
    public void testSaveAndFind() {
        Book book = new Book();
        book.setTitle("Spring Boot Guide");
        bookRepository.save(book);

        Book found = bookRepository.findByTitle("Spring Boot Guide");
        assertNotNull(found);
        assertEquals("Spring Boot Guide", found.getTitle());
    }
}
OutputSuccess
Important Notes

@DataJpaTest uses an in-memory database by default, so your tests run fast and isolated.

If you want to use your real database, add @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE).

Remember to keep tests small and focused on repository logic only.

Summary

@DataJpaTest is for testing JPA repositories without starting the full app.

It sets up an in-memory database and repository beans automatically.

Use it to write fast, focused tests for your database access code.