- Spring Boot JPA - Home
- Spring Boot JPA - Overview
- Spring Boot JPA - Environment Setup
- Spring Boot JPA - Architecture
- Spring Boot JPA vs Hibernate
- Spring Boot JPA - Application Setup
- Spring Boot JPA - Unit Test Repository
- Spring Boot JPA - Methods
- Spring Boot JPA - Custom Methods
- Spring Boot JPA - Named Query
- Spring Boot JPA - Custom Query
- Spring Boot JPA - Native Query
Spring Boot JPA Resources
Spring Boot & JPA - Unit Test Repository
To test a Repository, we need the following annotation and classes −
@SpringBootTest(classes = SprintBootH2Application.class) − Configure the Spring Boot application.
@Transactional − To mark repository to do CRUD Operation capable.
@Autowired private EmployeeRepository employeeRepository − EmployeeRepository object to be tested.
Example - Unit testing a JPA Repository
Following is the complete code of EmployeeRepositoryTest.
EmployeeRepositoryTest.java
package com.tutorialspoint.repository;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.tutorialspoint.entity.Employee;
import com.tutorialspoint.springboot_h2.SpringbootH2Application;
import jakarta.transaction.Transactional;
@Transactional
@SpringBootTest(classes = SpringbootH2Application.class)
public class {
@Autowired
private EmployeeRepository employeeRepository;
@Test
public void testFindById() {
Employee employee = getEmployee();
employeeRepository.save(employee);
Employee result = employeeRepository.findById(employee.getId()).get();
assertEquals(employee.getId(), result.getId());
}
@Test
public void testFindAll() {
Employee employee = getEmployee();
employeeRepository.save(employee);
List<Employee> result = new ArrayList<>();
employeeRepository.findAll().forEach(e -> result.add(e));
assertEquals(result.size(), 1);
}
@Test
public void testSave() {
Employee employee = getEmployee();
employeeRepository.save(employee);
Employee found = employeeRepository.findById(employee.getId()).get();
assertEquals(employee.getId(), found.getId());
}
@Test
public void testDeleteById() {
Employee employee = getEmployee();
employeeRepository.save(employee);
employeeRepository.deleteById(employee.getId());
List<Employee> result = new ArrayList<>();
employeeRepository.findAll().forEach(e -> result.add(e));
assertEquals(result.size(), 0);
}
private Employee getEmployee() {
Employee employee = new Employee();
employee.setId(1);
employee.setName("Mahesh");
employee.setAge(30);
employee.setEmail("mahesh@test.com");
return employee;
}
}
Run the test cases as JUnit Test
Output
Right Click on the file in eclipse and select Run a JUnit Test and verify the result.
Run the test cases using Maven Command.
Run the Test cases using Run As > Maven Test.
[INFO] Scanning for projects... ... [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.tutorialspoint.controller.[1mEmployeeControllerTest[m 21:31:59.404 [main] INFO org.springframework.boot.devtools.restart.RestartApplicationListener -- Restart disabled due to context in which it is running . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.5.6) 2025-10-01T21:31:59.827+05:30 INFO 36064 --- [springboot-h2] [ main] c.t.controller.EmployeeControllerTest : Starting EmployeeControllerTest using Java 21.0.6 with PID 36064 (started by mahes in D:\Projects\springboot-h2) 2025-10-01T21:31:59.832+05:30 INFO 36064 --- [springboot-h2] [ main] c.t.controller.EmployeeControllerTest : No active profile set, falling back to 1 default profile: "default" 2025-10-01T21:32:00.829+05:30 INFO 36064 --- [springboot-h2] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2025-10-01T21:32:00.869+05:30 INFO 36064 --- [springboot-h2] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 29 ms. Found 1 JPA repository interface. Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build as described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org.mockito/org/mockito/Mockito.html#0.3 OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended 2025-10-01T21:32:02.988+05:30 INFO 36064 --- [springboot-h2] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2025-10-01T21:32:03.441+05:30 INFO 36064 --- [springboot-h2] [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:testdb user=SA 2025-10-01T21:32:03.451+05:30 INFO 36064 --- [springboot-h2] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2025-10-01T21:32:03.671+05:30 INFO 36064 --- [springboot-h2] [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2025-10-01T21:32:03.824+05:30 INFO 36064 --- [springboot-h2] [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.6.29.Final 2025-10-01T21:32:03.916+05:30 INFO 36064 --- [springboot-h2] [ main] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled 2025-10-01T21:32:04.241+05:30 INFO 36064 --- [springboot-h2] [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer ... 2025-10-01T21:32:08.343+05:30 INFO 36064 --- [springboot-h2] [ main] c.t.repository.EmployeeRepositoryTest : Started EmployeeRepositoryTest in 0.587 seconds (process running for 10.161) ... [INFO] Results: [INFO] [INFO] [1;32mTests run: 14, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 15.223 s [INFO] Finished at: 2025-10-01T21:32:09+05:30 [INFO] [1m------------------------------------------------------------------------[m
Advertisements