- Spring Boot JPA 教程
- Spring Boot JPA - 首页
- Spring Boot JPA - 概述
- Spring Boot JPA - 环境设置
- Spring Boot JPA - 架构
- Spring Boot JPA 与 Hibernate
- Spring Boot JPA - 应用程序设置
- Spring Boot JPA - 单元测试存储库
- Spring Boot JPA - 方法
- Spring Boot JPA - 自定义方法
- Spring Boot JPA - 命名查询
- Spring Boot JPA - 自定义查询
- Spring Boot JPA - 原生查询
- Spring Boot JPA 实用资源
- Spring Boot JPA - 快速指南
- Spring Boot JPA - 实用资源
- Spring Boot JPA - 讨论
Spring Boot JPA - 自定义查询
有时会出现这种情况,我们需要一个自定义查询来满足一个测试用例。我们可以使用 @Query 注释在存储库中指定查询。下面是一个示例。在此示例中,我们正在使用 JPQL(Java 持久性查询语言)。
我们在 JPA 命名查询 章节的存储库中添加了名称查询自定义方法。现在,让我们使用 @Query 添加另一个方法并对其进行测试。
存储库 - EmployeeRepository.java
添加一个方法来按名称获取员工列表。
package com.tutorialspoint.repository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import com.tutorialspoint.entity.Employee; @Repository public interface EmployeeRepository extends CrudRepository<Employee, Integer> { public List<Employee> findByName(String name); public List<Employee> findByAge(int age); public Employee findByEmail(String email); @Query(value = "SELECT e FROM Employee e ORDER BY name") public List<Employee> findAllSortedByName(); }
让我们在测试文件中添加他们的测试用例来测试添加的方法。下方文件中的最后两个方法会测试添加的自定义查询方法。
以下是 EmployeeRepositoryTest 的完整代码。
package com.tutorialspoint.repository; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.ArrayList; import java.util.List; import javax.transaction.Transactional; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.SpringExtension; import com.tutorialspoint.entity.Employee; import com.tutorialspoint.sprintbooth2.SprintBootH2Application; @ExtendWith(SpringExtension.class) @Transactional @SpringBootTest(classes = SprintBootH2Application.class) public class EmployeeRepositoryTest { @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("[email protected]"); return employee; } @Test public void testFindByName() { Employee employee = getEmployee(); employeeRepository.save(employee); List<Employee> result = new ArrayList<>(); employeeRepository.findByName(employee.getName()).forEach(e -> result.add(e)); assertEquals(result.size(), 1); } @Test public void testFindByAge() { Employee employee = getEmployee(); employeeRepository.save(employee); List<Employee> result = new ArrayList<>(); employeeRepository.findByAge(employee.getAge()).forEach(e -> result.add(e)); assertEquals(result.size(), 1); } @Test public void testFindByEmail() { Employee employee = getEmployee(); employeeRepository.save(employee); Employee result = employeeRepository.findByEmail(employee.getEmail()); assertNotNull(result); } @Test public void testFindAllSortedByName() { Employee employee = getEmployee(); Employee employee1 = new Employee(); employee1.setId(2); employee1.setName("Aarav"); employee1.setAge(20); employee1.setEmail("[email protected]"); employeeRepository.save(employee); employeeRepository.save(employee1); List<Employee> result = employeeRepository.findAllSortedByName(); assertEquals(employee1.getName(), result.get(0).getName()); } }
运行测试用例
右键单击 Eclipse 中的文件,然后选择运行 JUnit 测试并验证结果。
广告