
本文演示如何使用Mockito的thenReturn()方法模拟服务来测试Spring Boot控制器。我们将创建一个简单的员工管理系统,包含Employee实体类、EmployeeService服务类和EmployeeController控制器类,并编写单元测试来验证控制器的功能。
1. 代码示例
Employee.java(实体类):
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;
}
}
EmployeeService.java(服务类):
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");
}
}
EmployeeController.java(控制器类):
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);
}
}
EmployeeControllerTest.java(单元测试类):
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. 测试流程
模板采用响应式设计,自动适应手机,电脑及平板显示;满足单一店铺外卖需求。功能:1.菜单分类管理2.菜品管理:菜品增加,删除,修改3.订单管理4.友情链接管理5.数据库备份6.文章模块:如:促销活动,帮助中心7.单页模块:如:企业信息,关于我们更强大的功能在开发中……安装方法:上传到网站根目录,运行http://www.***.com/install 自动
测试方法testGetEmployee包含三个步骤:
-
Arrange (准备): 使用
when().thenReturn()设置模拟服务的返回值。 -
Act (执行): 调用
employeeController.getEmployee("1")方法。 -
Assert (断言): 使用
assertNotNull()、assertEquals()验证返回的Employee对象是否符合预期,并使用verify()验证employeeService.getEmployeeById("1")方法是否被调用了一次。
4. 依赖注入和验证
@Mock注解用于创建EmployeeService的模拟对象,@InjectMocks注解将模拟的EmployeeService注入到EmployeeController中。verify()方法用于验证模拟对象的方法调用次数,确保测试覆盖了预期的代码路径。
通过这个例子,您可以理解如何在Spring Boot应用中使用Mockito的thenReturn()方法来模拟服务,并编写有效的单元测试,从而提高代码质量和可维护性。 无需启动整个Spring Boot应用,就能测试控制器的逻辑。









