本文演示如何使用Mockito的thenReturn()方法模拟服务来测试Spring Boot控制器。我们将创建一个简单的员工管理系统,包含Employee实体类、EmployeeService服务类和EmployeeController控制器类,并编写单元测试来验证控制器的功能。
1. 代码示例
package com.example.demo.model; public class Employee { private String id; private String name; // constructors, getters, and setters public Employee(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.example.demo.service; import com.example.demo.model.Employee; import org.springframework.stereotype.Service; @Service public class EmployeeService { public Employee getEmployeeById(String id) { // 模拟从数据库获取员工信息 return new Employee(id, "Default Name"); } }
package com.example.demo.controller; import com.example.demo.model.Employee; import com.example.demo.service.EmployeeService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class EmployeeController { private final EmployeeService employeeService; public EmployeeController(EmployeeService employeeService) { this.employeeService = employeeService; } @GetMapping("/employees/{id}") public Employee getEmployee(@PathVariable String id) { return employeeService.getEmployeeById(id); } }
package com.example.demo.controller; import com.example.demo.model.Employee; import com.example.demo.service.EmployeeService; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) // 使用Mockito扩展 class EmployeeControllerTest { @Mock private EmployeeService employeeService; @InjectMocks private EmployeeController employeeController; @Test void testGetEmployee() { // Arrange: 使用when().thenReturn()模拟服务行为 when(employeeService.getEmployeeById("1")).thenReturn(new Employee("1", "John Doe")); // Act: 调用控制器方法 Employee employee = employeeController.getEmployee("1"); // Assert: 验证返回的对象 assertNotNull(employee); assertEquals("1", employee.getId()); assertEquals("John Doe", employee.getName()); // 验证模拟的服务是否被调用 verify(employeeService, times(1)).getEmployeeById("1"); } }
2. when().thenReturn() 的作用
when(employeeService.getEmployeeById("1")).thenReturn(new Employee("1", "John Doe")); 这行代码使用Mockito的when()方法指定当employeeService.getEmployeeById("1")被调用时,应该返回一个新的Employee对象,其ID为"1",姓名为"John Doe"。这模拟了服务从数据库获取员工信息的场景。
3. 测试流程
测试方法testGetEmployee包含三个步骤:
4. 依赖注入和验证
@Mock注解用于创建EmployeeService的模拟对象,@InjectMocks注解将模拟的EmployeeService注入到EmployeeController中。verify()方法用于验证模拟对象的方法调用次数,确保测试覆盖了预期的代码路径。
通过这个例子,您可以理解如何在Spring Boot应用中使用Mockito的thenReturn()方法来模拟服务,并编写有效的单元测试,从而提高代码质量和可维护性。 无需启动整个Spring Boot应用,就能测试控制器的逻辑。
以上就是Mockito 示例中的 thenReturn() 方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号