
本文演示如何使用Mockito的thenReturn()方法模拟服务来测试Spring Boot控制器。我们将创建一个简单的员工管理系统,包含Employee实体类、EmployeeService服务类和EmployeeController控制器类,并编写单元测试来验证控制器的功能。
1. 代码示例
Employee.java (实体类):<code class="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;
}
}</code>EmployeeService.java (服务类):<code class="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");
}
}</code>EmployeeController.java (控制器类):<code class="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);
}
}</code>EmployeeControllerTest.java (单元测试类):<code class="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");
}
}</code>2. when().thenReturn() 的作用
when(employeeService.getEmployeeById("1")).thenReturn(new Employee("1", "John Doe")); 这行代码使用Mockito的when()方法指定当employeeService.getEmployeeById("1")被调用时,应该返回一个新的Employee对象,其ID为"1",姓名为"John Doe"。这模拟了服务从数据库获取员工信息的场景。
3. 测试流程
918 天蓝型企业展示系统旨为打造一个最简单漂亮大方的网站,主打展示型。该程序前台页面结构比较简单,但页面美观十分值得赞赏。前台栏目有:首 页、公司简介、服务项目、工程案例、新闻中心、联系我们。网站以天蓝色系为主,flash也很具特色,底部加入了漂亮大气的百度搜索框模块。前台页面结构简洁明了又别树一帜。 网站后台的栏目分为:系统基本信息 信息管理 产品系统 系统插件 系统管理。 后台除了这
0
测试方法testGetEmployee包含三个步骤:
when().thenReturn()设置模拟服务的返回值。employeeController.getEmployee("1")方法。assertNotNull()、assertEquals()验证返回的Employee对象是否符合预期,并使用verify()验证employeeService.getEmployeeById("1")方法是否被调用了一次。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号