测试用例

This commit is contained in:
lvys 2025-06-06 11:16:32 +08:00
parent 65a58e5e53
commit 15800b84a6
6 changed files with 618 additions and 0 deletions

View File

@ -0,0 +1,219 @@
package com.dkl.large.controller;
import com.dkl.large.domain.DklWarningInformationProcess;
import com.dkl.large.service.IDklWarningInformationProcessService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* 预警信息处置流程Controller测试类
*
* @author Dkl
* @date 2025-06-17
*/
@RunWith(MockitoJUnitRunner.class)
public class DklWarningInformationProcessControllerTest {
// 模拟MVC环境
private MockMvc mockMvc;
// 待测试的Controller
@InjectMocks
private DklWarningInformationProcessController processController;
// 模拟Service层
@Mock
private IDklWarningInformationProcessService processService;
// JSON序列化/反序列化工具
private ObjectMapper objectMapper = new ObjectMapper();
// 测试用例数据
private DklWarningInformationProcess testProcess;
@Before
public void setUp() {
// 初始化MockMvc
mockMvc = MockMvcBuilders.standaloneSetup(processController).build();
// 初始化测试数据
testProcess = new DklWarningInformationProcess();
testProcess.setId(1L);
testProcess.setDelFlag("0");
testProcess.setCreateTime(new Date());
testProcess.setCreateBy("testUser");
testProcess.setUpdateTime(new Date());
testProcess.setUpdateBy("testUser");
}
/**
* 测试查询列表接口 (/large/process/list)
*/
@Test
@WithMockUser // 模拟登录用户
public void testList() throws Exception {
// 构造返回数据
List<DklWarningInformationProcess> processList = new ArrayList<>();
processList.add(testProcess);
// Mock Service层方法
when(processService.selectDklWarningInformationProcessList(any(DklWarningInformationProcess.class)))
.thenReturn(processList);
// 执行请求并验证结果
mockMvc.perform(get("/large/process/list")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) // 响应状态200
.andExpect(jsonPath("$.rows").isArray()) // 返回包含rows数组
.andExpect(jsonPath("$.rows[0].id").value(1L)); // 验证数据
// 验证Service方法被调用
verify(processService, times(1)).selectDklWarningInformationProcessList(any(DklWarningInformationProcess.class));
}
/**
* 测试查询全部列表接口 (/large/process/listAll)
*/
@Test
@WithMockUser
public void testListAll() throws Exception {
// 构造返回数据
List<DklWarningInformationProcess> processList = new ArrayList<>();
processList.add(testProcess);
// Mock Service层方法
when(processService.selectDklWarningInformationProcessList(any(DklWarningInformationProcess.class)))
.thenReturn(processList);
// 执行请求并验证结果
mockMvc.perform(get("/large/process/listAll")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows").isArray())
.andExpect(jsonPath("$.rows[0].createBy").value("testUser"));
verify(processService, times(1)).selectDklWarningInformationProcessList(any(DklWarningInformationProcess.class));
}
/**
* 测试导出接口 (/large/process/export)
*/
@Test
@WithMockUser
public void testExport() throws Exception {
// 构造返回数据
List<DklWarningInformationProcess> processList = new ArrayList<>();
processList.add(testProcess);
// Mock Service层方法
when(processService.selectDklWarningInformationProcessList(any(DklWarningInformationProcess.class)))
.thenReturn(processList);
// 执行导出请求并验证响应
MockHttpServletResponse response = mockMvc.perform(post("/large/process/export")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn()
.getResponse();
// 验证响应头Excel导出
org.junit.Assert.assertTrue(response.getContentType().contains("application/vnd.ms-excel"));
org.junit.Assert.assertEquals("attachment;filename*=UTF-8''预警信息处置流程数据.xlsx", response.getHeader("Content-Disposition"));
verify(processService, times(1)).selectDklWarningInformationProcessList(any(DklWarningInformationProcess.class));
}
/**
* 测试获取详情接口 (/large/process/{id})
*/
@Test
@WithMockUser
public void testGetInfo() throws Exception {
// Mock Service层方法
when(processService.selectDklWarningInformationProcessById(1L)).thenReturn(testProcess);
// 执行请求并验证
mockMvc.perform(get("/large/process/1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200)) // 假设AjaxResult的code为200表示成功
.andExpect(jsonPath("$.data.id").value(1L));
verify(processService, times(1)).selectDklWarningInformationProcessById(1L);
}
/**
* 测试新增接口 (/large/process)
*/
@Test
@WithMockUser
public void testAdd() throws Exception {
// Mock Service层方法
when(processService.insertDklWarningInformationProcess(any(DklWarningInformationProcess.class))).thenReturn(1);
// 执行POST请求
mockMvc.perform(post("/large/process")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(testProcess)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200));
verify(processService, times(1)).insertDklWarningInformationProcess(any(DklWarningInformationProcess.class));
}
/**
* 测试修改接口 (/large/process)
*/
@Test
@WithMockUser
public void testEdit() throws Exception {
// Mock Service层方法
when(processService.updateDklWarningInformationProcess(any(DklWarningInformationProcess.class))).thenReturn(1);
// 执行PUT请求
mockMvc.perform(put("/large/process")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(testProcess)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200));
verify(processService, times(1)).updateDklWarningInformationProcess(any(DklWarningInformationProcess.class));
}
/**
* 测试删除接口 (/large/process/{ids})
*/
@Test
@WithMockUser
public void testRemove() throws Exception {
// Mock Service层方法
when(processService.deleteDklWarningInformationProcessByIds(any(Long[].class))).thenReturn(1);
// 执行DELETE请求
mockMvc.perform(delete("/large/process/1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200));
verify(processService, times(1)).deleteDklWarningInformationProcessByIds(any(Long[].class));
}
}

View File

@ -0,0 +1,62 @@
package com.dkl.large.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.dkl.common.annotation.Excel;
import com.dkl.common.core.domain.BaseEntity;
/**
* 预警信息处置流程对象 dkl_warning_information_process
*
* @author Dkl
* @date 2025-06-17
*/
@Data
public class DklWarningInformationProcess extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** id */
private Long id;
/** 预警唯一标识符 */
@Excel(name = "预警唯一标识符")
private String warningSigns;
/** 委派人 */
@Excel(name = "委派人")
private Long delegatePersonnel;
/** 委派时间 */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "委派时间", width = 30, dateFormat = "yyyy-MM-dd")
private Date delegationTime;
/** 委派部门 */
@Excel(name = "委派部门")
private Long delegationDept;
/** 处置措施 */
@Excel(name = "处置措施")
private String disposalMeasures;
/** 处置结果 */
@Excel(name = "处置结果")
private String disposalResults;
/** 处理状态 */
@Excel(name = "处理状态")
private String disposalStatus;
/** 删除标志0代表存在 2代表删除 */
private String delFlag;
/** 处置id */
@Excel(name = "处置id")
private Long handleId;
}

View File

@ -0,0 +1,62 @@
package com.dkl.large.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.dkl.common.annotation.Excel;
import com.dkl.common.core.domain.BaseEntity;
/**
* 预警信息处置流程对象 dkl_warning_information_process
*
* @author Dkl
* @date 2025-06-17
*/
@Data
public class DklWarningInformationProcess extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** id */
private Long id;
/** 预警唯一标识符 */
@Excel(name = "预警唯一标识符")
private String warningSigns;
/** 委派人 */
@Excel(name = "委派人")
private Long delegatePersonnel;
/** 委派时间 */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "委派时间", width = 30, dateFormat = "yyyy-MM-dd")
private Date delegationTime;
/** 委派部门 */
@Excel(name = "委派部门")
private Long delegationDept;
/** 处置措施 */
@Excel(name = "处置措施")
private String disposalMeasures;
/** 处置结果 */
@Excel(name = "处置结果")
private String disposalResults;
/** 处理状态 */
@Excel(name = "处理状态")
private String disposalStatus;
/** 删除标志0代表存在 2代表删除 */
private String delFlag;
/** 处置id */
@Excel(name = "处置id")
private Long handleId;
}

View File

@ -0,0 +1,61 @@
package com.dkl.large.service;
import java.util.List;
import com.dkl.large.domain.DklWarningInformationProcess;
/**
* 预警信息处置流程Service接口
*
* @author Dkl
* @date 2025-06-17
*/
public interface IDklWarningInformationProcessService
{
/**
* 查询预警信息处置流程
*
* @param id 预警信息处置流程主键
* @return 预警信息处置流程
*/
public DklWarningInformationProcess selectDklWarningInformationProcessById(Long id);
/**
* 查询预警信息处置流程列表
*
* @param dklWarningInformationProcess 预警信息处置流程
* @return 预警信息处置流程集合
*/
public List<DklWarningInformationProcess> selectDklWarningInformationProcessList(DklWarningInformationProcess dklWarningInformationProcess);
/**
* 新增预警信息处置流程
*
* @param dklWarningInformationProcess 预警信息处置流程
* @return 结果
*/
public int insertDklWarningInformationProcess(DklWarningInformationProcess dklWarningInformationProcess);
/**
* 修改预警信息处置流程
*
* @param dklWarningInformationProcess 预警信息处置流程
* @return 结果
*/
public int updateDklWarningInformationProcess(DklWarningInformationProcess dklWarningInformationProcess);
/**
* 批量删除预警信息处置流程
*
* @param ids 需要删除的预警信息处置流程主键集合
* @return 结果
*/
public int deleteDklWarningInformationProcessByIds(Long[] ids);
/**
* 删除预警信息处置流程信息
*
* @param id 预警信息处置流程主键
* @return 结果
*/
public int deleteDklWarningInformationProcessById(Long id);
}

View File

@ -0,0 +1,102 @@
package com.dkl.large.service.impl;
import java.util.Date;
import java.util.List;
import com.dkl.common.utils.DateUtils;
import com.dkl.large.domain.DklWarningInformationHandle;
import com.dkl.large.mapper.DklWarningInformationHandleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkl.large.mapper.DklWarningInformationProcessMapper;
import com.dkl.large.domain.DklWarningInformationProcess;
import com.dkl.large.service.IDklWarningInformationProcessService;
import org.springframework.transaction.annotation.Transactional;
/**
* 预警信息处置流程Service业务层处理
*
* @author Dkl
* @date 2025-06-17
*/
@Service
public class DklWarningInformationProcessServiceImpl implements IDklWarningInformationProcessService
{
@Autowired
private DklWarningInformationProcessMapper dklWarningInformationProcessMapper;
@Autowired
private DklWarningInformationHandleMapper dklWarningInformationHandleMapper;
/**
* 查询预警信息处置流程
*
* @param id 预警信息处置流程主键
* @return 预警信息处置流程
*/
@Override
public DklWarningInformationProcess selectDklWarningInformationProcessById(Long id)
{
return dklWarningInformationProcessMapper.selectDklWarningInformationProcessById(id);
}
/**
* 查询预警信息处置流程列表
*
* @param dklWarningInformationProcess 预警信息处置流程
* @return 预警信息处置流程
*/
@Override
public List<DklWarningInformationProcess> selectDklWarningInformationProcessList(DklWarningInformationProcess dklWarningInformationProcess)
{
return dklWarningInformationProcessMapper.selectDklWarningInformationProcessList(dklWarningInformationProcess);
}
/**
* 新增预警信息处置流程
*
* @param dklWarningInformationProcess 预警信息处置流程
* @return 结果
*/
@Override
@Transactional
public int insertDklWarningInformationProcess(DklWarningInformationProcess dklWarningInformationProcess)
{
return dklWarningInformationProcessMapper.insertDklWarningInformationProcess(dklWarningInformationProcess);
}
/**
* 修改预警信息处置流程
*
* @param dklWarningInformationProcess 预警信息处置流程
* @return 结果
*/
@Override
public int updateDklWarningInformationProcess(DklWarningInformationProcess dklWarningInformationProcess)
{
dklWarningInformationProcess.setUpdateTime(DateUtils.getNowDate());
return dklWarningInformationProcessMapper.updateDklWarningInformationProcess(dklWarningInformationProcess);
}
/**
* 批量删除预警信息处置流程
*
* @param ids 需要删除的预警信息处置流程主键
* @return 结果
*/
@Override
public int deleteDklWarningInformationProcessByIds(Long[] ids)
{
return dklWarningInformationProcessMapper.deleteDklWarningInformationProcessByIds(ids);
}
/**
* 删除预警信息处置流程信息
*
* @param id 预警信息处置流程主键
* @return 结果
*/
@Override
public int deleteDklWarningInformationProcessById(Long id)
{
return dklWarningInformationProcessMapper.deleteDklWarningInformationProcessById(id);
}
}

View File

@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dkl.large.mapper.DklWarningInformationProcessMapper">
<resultMap type="DklWarningInformationProcess" id="DklWarningInformationProcessResult">
<result property="id" column="id" />
<result property="warningSigns" column="warning_signs" />
<result property="delegatePersonnel" column="delegate_personnel" />
<result property="delegationTime" column="delegation_time" />
<result property="delegationDept" column="delegation_dept" />
<result property="disposalMeasures" column="disposal_measures" />
<result property="disposalResults" column="disposal_results" />
<result property="disposalStatus" column="disposal_status" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="delFlag" column="del_flag" />
<result property="handleId" column="handle_id" />
</resultMap>
<sql id="selectDklWarningInformationProcessVo">
select id, warning_signs, delegate_personnel, delegation_time, delegation_dept, disposal_measures, disposal_results, disposal_status, create_by, create_time, update_by, update_time, del_flag, handle_id from dkl_warning_information_process
</sql>
<select id="selectDklWarningInformationProcessList" parameterType="DklWarningInformationProcess" resultMap="DklWarningInformationProcessResult">
<include refid="selectDklWarningInformationProcessVo"/>
<where>
<if test="warningSigns != null and warningSigns != ''"> and warning_signs = #{warningSigns}</if>
<if test="delegatePersonnel != null "> and delegate_personnel = #{delegatePersonnel}</if>
<if test="delegationTime != null "> and delegation_time = #{delegationTime}</if>
<if test="delegationDept != null "> and delegation_dept = #{delegationDept}</if>
<if test="disposalMeasures != null and disposalMeasures != ''"> and disposal_measures = #{disposalMeasures}</if>
<if test="disposalResults != null and disposalResults != ''"> and disposal_results = #{disposalResults}</if>
<if test="disposalStatus != null and disposalStatus != ''"> and disposal_status = #{disposalStatus}</if>
<if test="handleId != null "> and handle_id = #{handleId}</if>
</where>
ORDER BY create_time
</select>
<select id="selectDklWarningInformationProcessById" parameterType="Long" resultMap="DklWarningInformationProcessResult">
<include refid="selectDklWarningInformationProcessVo"/>
where id = #{id}
</select>
<insert id="insertDklWarningInformationProcess" parameterType="DklWarningInformationProcess">
insert into dkl_warning_information_process
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="warningSigns != null">warning_signs,</if>
<if test="delegatePersonnel != null">delegate_personnel,</if>
<if test="delegationTime != null">delegation_time,</if>
<if test="delegationDept != null">delegation_dept,</if>
<if test="disposalMeasures != null">disposal_measures,</if>
<if test="disposalResults != null">disposal_results,</if>
<if test="disposalStatus != null">disposal_status,</if>
<if test="createBy != null">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="delFlag != null">del_flag,</if>
<if test="handleId != null">handle_id,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="warningSigns != null">#{warningSigns},</if>
<if test="delegatePersonnel != null">#{delegatePersonnel},</if>
<if test="delegationTime != null">#{delegationTime},</if>
<if test="delegationDept != null">#{delegationDept},</if>
<if test="disposalMeasures != null">#{disposalMeasures},</if>
<if test="disposalResults != null">#{disposalResults},</if>
<if test="disposalStatus != null">#{disposalStatus},</if>
<if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="delFlag != null">#{delFlag},</if>
<if test="handleId != null">#{handleId},</if>
</trim>
</insert>
<update id="updateDklWarningInformationProcess" parameterType="DklWarningInformationProcess">
update dkl_warning_information_process
<trim prefix="SET" suffixOverrides=",">
<if test="warningSigns != null">warning_signs = #{warningSigns},</if>
<if test="delegatePersonnel != null">delegate_personnel = #{delegatePersonnel},</if>
<if test="delegationTime != null">delegation_time = #{delegationTime},</if>
<if test="delegationDept != null">delegation_dept = #{delegationDept},</if>
<if test="disposalMeasures != null">disposal_measures = #{disposalMeasures},</if>
<if test="disposalResults != null">disposal_results = #{disposalResults},</if>
<if test="disposalStatus != null">disposal_status = #{disposalStatus},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="delFlag != null">del_flag = #{delFlag},</if>
<if test="handleId != null">handle_id = #{handleId},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteDklWarningInformationProcessById" parameterType="Long">
delete from dkl_warning_information_process where id = #{id}
</delete>
<delete id="deleteDklWarningInformationProcessByIds" parameterType="String">
delete from dkl_warning_information_process where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>