为了对 isGlobalQuery
方法进行单元测试,我们需要使用一个单元测试框架,如 JUnit,并结合 mocking 框架(如 Mockito)来模拟依赖对象的行为。下面是一个详细的单元测试示例,假设我们使用的是 JUnit 5 和 Mockito。
1. 引入依赖
首先,确保你的项目中已经引入了 JUnit 和 Mockito 的依赖。如果你使用的是 Maven,可以在 pom.xml
中添加以下依赖:
<dependencies>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.6.28</version>
<scope>test</scope>
</dependency>
<!-- Mockito for JUnit 5 -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.6.28</version>
<scope>test</scope>
</dependency>
</dependencies>
2. 编写单元测试
假设 DepartmentServiceImpl
是我们要测试的类,我们可以编写如下单元测试:
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.example.service.DepartmentServiceImpl;
import com.example.mapper.DepartmentMapper;
import com.example.request.QueryDepartmentGlobalQueryRequest;
import com.example.response.QueryDepartmentGlobalQueryResponse;
import com.example.entity.Department;
import com.example.utils.RespUtils;
import com.example.enums.ErrorCodeEnum;
public class DepartmentServiceImplTest {
@Mock
private DepartmentMapper departmentMapper;
@InjectMocks
private DepartmentServiceImpl departmentService;
@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
public void testIsGlobalQuery_DepartmentExists_GlobalQueryTrue() {
// 准备数据
String departmentId = "123";
Department department = new Department();
department.setDepartmentId(departmentId);
department.setGlobalQuery(true);
department.setQueryEffectiveDate(new Date());
department.setQueryPermanentFlag(true);
// 模拟行为
when(departmentMapper.selectOneByExample(any())).thenReturn(department);
// 执行方法
QueryDepartmentGlobalQueryRequest request = new QueryDepartmentGlobalQueryRequest();
request.setDepartmentId(departmentId);
QueryDepartmentGlobalQueryResponse response = departmentService.isGlobalQuery(request);
// 断言结果
assertNotNull(response);
assertTrue(response.getGlobalQueryFlag());
assertEquals(department.getQueryEffectiveDate(), response.getEffectiveDate());
assertTrue(response.getPermanentFlag());
}
@Test
public void testIsGlobalQuery_DepartmentExists_GlobalQueryFalse() {
// 准备数据
String departmentId = "123";
Department department = new Department();
department.setDepartmentId(departmentId);
department.setGlobalQuery(false);
department.setQueryEffectiveDate(new Date());
department.setQueryPermanentFlag(true);
// 模拟行为
when(departmentMapper.selectOneByExample(any())).thenReturn(department);
// 执行方法
QueryDepartmentGlobalQueryRequest request = new QueryDepartmentGlobalQueryRequest();
request.setDepartmentId(departmentId);
QueryDepartmentGlobalQueryResponse response = departmentService.isGlobalQuery(request);
// 断言结果
assertNotNull(response);
assertFalse(response.getGlobalQueryFlag());
assertEquals(department.getQueryEffectiveDate(), response.getEffectiveDate());
assertTrue(response.getPermanentFlag());
}
@Test
public void testIsGlobalQuery_DepartmentNotExists() {
// 准备数据
String departmentId = "123";
// 模拟行为
when(departmentMapper.selectOneByExample(any())).thenReturn(null);
// 执行方法
QueryDepartmentGlobalQueryRequest request = new QueryDepartmentGlobalQueryRequest();
request.setDepartmentId(departmentId);
QueryDepartmentGlobalQueryResponse response = departmentService.isGlobalQuery(request);
// 断言结果
assertNotNull(response);
assertEquals(ErrorCodeEnum.DEPARTMENT_NOT_EXIST.getCode(), response.getCode());
}
@Test
public void testIsGlobalQuery_GlobalQueryExpired() {
// 准备数据
String departmentId = "123";
Department department = new Department();
department.setDepartmentId(departmentId);
department.setGlobalQuery(true);
department.setQueryEffectiveDate(Date.from(Instant.now().minus(1, ChronoUnit.DAYS)));
department.setQueryPermanentFlag(false);
// 模拟行为
when(departmentMapper.selectOneByExample(any())).thenReturn(department);
// 执行方法
QueryDepartmentGlobalQueryRequest request = new QueryDepartmentGlobalQueryRequest();
request.setDepartmentId(departmentId);
QueryDepartmentGlobalQueryResponse response = departmentService.isGlobalQuery(request);
// 断言结果
assertNotNull(response);
assertFalse(response.getGlobalQueryFlag());
assertEquals(department.getQueryEffectiveDate(), response.getEffectiveDate());
assertFalse(response.getPermanentFlag());
}
}
3. 测试说明
- testIsGlobalQuery_DepartmentExists_GlobalQueryTrue: 测试部门存在且
globalQuery
为true
的情况。 - testIsGlobalQuery_DepartmentExists_GlobalQueryFalse: 测试部门存在且
globalQuery
为false
的情况。 - testIsGlobalQuery_DepartmentNotExists: 测试部门不存在的情况。
- testIsGlobalQuery_GlobalQueryExpired: 测试
globalQuery
有效期限已过期的情况。
4. 运行测试
你可以使用 IDE(如 IntelliJ IDEA 或 Eclipse)中的内置工具运行这些测试,也可以通过命令行运行:
mvn test
这将运行所有的单元测试,并输出测试结果。
标签:11,request,departmentId,org,department,import,response From: https://www.cnblogs.com/lmzzr24/p/18544103