测试用例
This commit is contained in:
parent
7b85f36269
commit
c86a8156b8
|
|
@ -0,0 +1,209 @@
|
|||
package com.dkl.large.controller;
|
||||
|
||||
import com.dkl.common.core.domain.AjaxResult;
|
||||
import com.dkl.common.core.page.TableDataInfo;
|
||||
import com.dkl.large.domain.DklSupervise;
|
||||
import com.dkl.large.service.IDklSuperviseService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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 org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* 督办信息Controller单元测试
|
||||
* 风格与之前保持一致:JUnit 5 + Mockito,覆盖所有核心接口与关键逻辑
|
||||
*
|
||||
* @author Dkl
|
||||
* @date 2025-11-03
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class DklSuperviseControllerTest {
|
||||
|
||||
// 待测试的Controller(自动注入Mock的Service依赖,解耦真实业务层)
|
||||
@InjectMocks
|
||||
private DklSuperviseController dklSuperviseController;
|
||||
|
||||
// 模拟Controller依赖的Service层(避免依赖真实数据库与业务逻辑)
|
||||
@Mock
|
||||
private IDklSuperviseService dklSuperviseService;
|
||||
|
||||
// 模拟HTTP响应对象(用于导出接口)
|
||||
private MockHttpServletResponse mockResponse;
|
||||
|
||||
// 测试核心实体数据(督办信息)
|
||||
private DklSupervise testSupervise;
|
||||
|
||||
// 测试实体列表数据
|
||||
private List<DklSupervise> testSuperviseList;
|
||||
|
||||
/**
|
||||
* 每个测试方法执行前初始化数据(保证测试用例独立性,避免相互干扰)
|
||||
*/
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// 1. 初始化模拟HTTP响应
|
||||
mockResponse = new MockHttpServletResponse();
|
||||
|
||||
// 2. 初始化单个督办信息实体
|
||||
testSupervise = new DklSupervise();
|
||||
testSupervise.setId(1L);
|
||||
testSupervise.setDelFlag("0");
|
||||
testSupervise.setCreateTime(new Date());
|
||||
testSupervise.setCreateBy("admin");
|
||||
testSupervise.setUpdateTime(new Date());
|
||||
testSupervise.setUpdateBy("admin");
|
||||
|
||||
// 3. 初始化督办信息列表
|
||||
testSuperviseList = new ArrayList<>();
|
||||
testSuperviseList.add(testSupervise);
|
||||
|
||||
// 4. 模拟Security上下文(解决getUsername()等权限方法依赖,同时满足@PreAuthorize注解校验)
|
||||
Authentication authentication = mock(Authentication.class);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
doReturn("admin").when(authentication).getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试:查询督办信息列表(list接口)
|
||||
*/
|
||||
@Test
|
||||
void testList() {
|
||||
// 步骤1:模拟Service层返回列表数据
|
||||
when(dklSuperviseService.selectDklSuperviseList(any(DklSupervise.class))).thenReturn(testSuperviseList);
|
||||
|
||||
// 步骤2:调用Controller接口执行测试
|
||||
TableDataInfo result = dklSuperviseController.list(testSupervise);
|
||||
|
||||
// 步骤3:断言结果(验证返回数据有效性)
|
||||
assertNotNull(result, "返回的TableDataInfo不能为null");
|
||||
assertEquals(1, result.getRows().size(), "返回的列表数据条数应为1");
|
||||
assertEquals(testSupervise.getId(), ((DklSupervise) result.getRows().get(0)).getId(), "返回的实体ID应与测试数据一致");
|
||||
|
||||
// 步骤4:验证Service方法被正确调用(确保流程无遗漏)
|
||||
verify(dklSuperviseService, times(1)).selectDklSuperviseList(any(DklSupervise.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试:导出督办信息列表(export接口,无返回值,验证流程有效性)
|
||||
*/
|
||||
@Test
|
||||
void testExport() {
|
||||
// 步骤1:模拟Service层返回列表数据
|
||||
when(dklSuperviseService.selectDklSuperviseList(any(DklSupervise.class))).thenReturn(testSuperviseList);
|
||||
|
||||
// 步骤2:调用导出接口(无返回值,验证无异常抛出即可)
|
||||
assertDoesNotThrow(() -> dklSuperviseController.export(mockResponse, testSupervise),
|
||||
"导出接口执行过程中不应抛出异常");
|
||||
|
||||
// 步骤3:验证Service方法被调用,且HTTP响应状态正常
|
||||
verify(dklSuperviseService, times(1)).selectDklSuperviseList(any(DklSupervise.class));
|
||||
assertEquals(200, mockResponse.getStatus(), "HTTP响应状态码应为200");
|
||||
assertTrue(mockResponse.getContentType().contains("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
|
||||
"响应内容应为Excel格式");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试:获取督办信息详细信息(getInfo接口)
|
||||
*/
|
||||
@Test
|
||||
void testGetInfo() {
|
||||
// 步骤1:模拟Service层返回单个实体数据
|
||||
when(dklSuperviseService.selectDklSuperviseById(anyLong())).thenReturn(testSupervise);
|
||||
|
||||
// 步骤2:调用Controller接口执行测试
|
||||
AjaxResult result = dklSuperviseController.getInfo(1L);
|
||||
|
||||
// 步骤3:断言结果
|
||||
assertNotNull(result, "返回的AjaxResult不能为null");
|
||||
assertTrue(result.isSuccess(), "接口应返回成功状态");
|
||||
assertEquals(testSupervise.getId(), ((DklSupervise) result.getData()).getId(), "返回的实体ID应与测试数据一致");
|
||||
|
||||
// 步骤4:验证Service方法调用
|
||||
verify(dklSuperviseService, times(1)).selectDklSuperviseById(1L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试:新增督办信息(add接口)
|
||||
*/
|
||||
@Test
|
||||
void testAdd() {
|
||||
// 步骤1:模拟Service层新增成功(返回1表示操作成功)
|
||||
when(dklSuperviseService.insertDklSupervise(any(DklSupervise.class))).thenReturn(1);
|
||||
|
||||
// 步骤2:调用Controller接口执行测试
|
||||
AjaxResult result = dklSuperviseController.add(testSupervise);
|
||||
|
||||
// 步骤3:断言结果
|
||||
assertNotNull(result, "返回的AjaxResult不能为null");
|
||||
assertTrue(result.isSuccess(), "新增接口应返回成功状态");
|
||||
assertEquals(1, result.getData(), "新增操作应返回1表示成功");
|
||||
|
||||
// 步骤4:验证实体系统字段被正确设置(核心业务逻辑校验)
|
||||
verify(dklSuperviseService).insertDklSupervise(argThat(supervise ->
|
||||
"0".equals(supervise.getDelFlag()) &&
|
||||
"admin".equals(supervise.getCreateBy()) &&
|
||||
supervise.getCreateTime() != null &&
|
||||
supervise.getUpdateTime() != null
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试:修改督办信息(edit接口,含@PreAuthorize权限校验)
|
||||
*/
|
||||
@Test
|
||||
void testEdit() {
|
||||
// 步骤1:模拟Service层修改成功(返回1表示操作成功)
|
||||
when(dklSuperviseService.updateDklSupervise(any(DklSupervise.class))).thenReturn(1);
|
||||
|
||||
// 步骤2:调用Controller接口执行测试
|
||||
AjaxResult result = dklSuperviseController.edit(testSupervise);
|
||||
|
||||
// 步骤3:断言结果
|
||||
assertNotNull(result, "返回的AjaxResult不能为null");
|
||||
assertTrue(result.isSuccess(), "修改接口应返回成功状态");
|
||||
assertEquals(1, result.getData(), "修改操作应返回1表示成功");
|
||||
|
||||
// 步骤4:验证实体更新字段被正确设置
|
||||
verify(dklSuperviseService).updateDklSupervise(argThat(supervise ->
|
||||
"admin".equals(supervise.getUpdateBy()) &&
|
||||
supervise.getUpdateTime() != null
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试:删除督办信息(remove接口,批量删除,含@PreAuthorize权限校验)
|
||||
*/
|
||||
@Test
|
||||
void testRemove() {
|
||||
// 步骤1:准备批量删除ID数组
|
||||
Long[] ids = {1L, 2L};
|
||||
|
||||
// 步骤2:模拟Service层删除成功(返回2表示删除2条数据)
|
||||
when(dklSuperviseService.deleteDklSuperviseByIds(any(Long[].class))).thenReturn(2);
|
||||
|
||||
// 步骤3:调用Controller接口执行测试
|
||||
AjaxResult result = dklSuperviseController.remove(ids);
|
||||
|
||||
// 步骤4:断言结果
|
||||
assertNotNull(result, "返回的AjaxResult不能为null");
|
||||
assertTrue(result.isSuccess(), "删除接口应返回成功状态");
|
||||
assertEquals(2, result.getData(), "删除操作应返回2表示成功删除2条数据");
|
||||
|
||||
// 步骤5:验证Service方法调用
|
||||
verify(dklSuperviseService, times(1)).deleteDklSuperviseByIds(any(Long[].class));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package com.dkl.large.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
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_supervise
|
||||
*
|
||||
* @author Dkl
|
||||
* @date 2025-11-03
|
||||
*/
|
||||
@Data
|
||||
public class DklSupervise extends BaseEntity
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** id */
|
||||
private Long id;
|
||||
|
||||
/** 预警信息id */
|
||||
@Excel(name = "预警信息id")
|
||||
private int warningId;
|
||||
|
||||
@TableField(exist = false)
|
||||
private String warningName;
|
||||
/** 类型 */
|
||||
@Excel(name = "类型")
|
||||
private String types;
|
||||
|
||||
/** 删除标志(0代表存在 2代表删除) */
|
||||
private String delFlag;
|
||||
|
||||
/** 内容 */
|
||||
@Excel(name = "内容")
|
||||
private String contents;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package com.dkl.large.mapper;
|
||||
|
||||
import com.dkl.large.domain.DklSupervise;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 督办信息Mapper接口
|
||||
*
|
||||
* @author Dkl
|
||||
* @date 2025-11-03
|
||||
*/
|
||||
public interface DklSuperviseMapper
|
||||
{
|
||||
/**
|
||||
* 查询督办信息
|
||||
*
|
||||
* @param id 督办信息主键
|
||||
* @return 督办信息
|
||||
*/
|
||||
public DklSupervise selectDklSuperviseById(Long id);
|
||||
|
||||
/**
|
||||
* 查询督办信息列表
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 督办信息集合
|
||||
*/
|
||||
public List<DklSupervise> selectDklSuperviseList(DklSupervise dklSupervise);
|
||||
|
||||
/**
|
||||
* 新增督办信息
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertDklSupervise(DklSupervise dklSupervise);
|
||||
|
||||
/**
|
||||
* 修改督办信息
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateDklSupervise(DklSupervise dklSupervise);
|
||||
|
||||
/**
|
||||
* 删除督办信息
|
||||
*
|
||||
* @param id 督办信息主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteDklSuperviseById(Long id);
|
||||
|
||||
/**
|
||||
* 批量删除督办信息
|
||||
*
|
||||
* @param ids 需要删除的数据主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteDklSuperviseByIds(Long[] ids);
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package com.dkl.large.service;
|
||||
|
||||
import com.dkl.large.domain.DklSupervise;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 督办信息Service接口
|
||||
*
|
||||
* @author Dkl
|
||||
* @date 2025-11-03
|
||||
*/
|
||||
public interface IDklSuperviseService
|
||||
{
|
||||
/**
|
||||
* 查询督办信息
|
||||
*
|
||||
* @param id 督办信息主键
|
||||
* @return 督办信息
|
||||
*/
|
||||
public DklSupervise selectDklSuperviseById(Long id);
|
||||
|
||||
/**
|
||||
* 查询督办信息列表
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 督办信息集合
|
||||
*/
|
||||
public List<DklSupervise> selectDklSuperviseList(DklSupervise dklSupervise);
|
||||
|
||||
/**
|
||||
* 新增督办信息
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertDklSupervise(DklSupervise dklSupervise);
|
||||
|
||||
/**
|
||||
* 修改督办信息
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateDklSupervise(DklSupervise dklSupervise);
|
||||
|
||||
/**
|
||||
* 批量删除督办信息
|
||||
*
|
||||
* @param ids 需要删除的督办信息主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteDklSuperviseByIds(Long[] ids);
|
||||
|
||||
/**
|
||||
* 删除督办信息信息
|
||||
*
|
||||
* @param id 督办信息主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteDklSuperviseById(Long id);
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
package com.dkl.large.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import com.dkl.common.utils.DateUtils;
|
||||
import com.dkl.large.domain.DklSupervise;
|
||||
import com.dkl.large.mapper.DklSuperviseMapper;
|
||||
import com.dkl.large.service.IDklSuperviseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* 督办信息Service业务层处理
|
||||
*
|
||||
* @author Dkl
|
||||
* @date 2025-11-03
|
||||
*/
|
||||
@Service
|
||||
public class DklSuperviseServiceImpl implements IDklSuperviseService
|
||||
{
|
||||
@Autowired
|
||||
private DklSuperviseMapper dklSuperviseMapper;
|
||||
|
||||
/**
|
||||
* 查询督办信息
|
||||
*
|
||||
* @param id 督办信息主键
|
||||
* @return 督办信息
|
||||
*/
|
||||
@Override
|
||||
public DklSupervise selectDklSuperviseById(Long id)
|
||||
{
|
||||
return dklSuperviseMapper.selectDklSuperviseById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询督办信息列表
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 督办信息
|
||||
*/
|
||||
@Override
|
||||
public List<DklSupervise> selectDklSuperviseList(DklSupervise dklSupervise)
|
||||
{
|
||||
return dklSuperviseMapper.selectDklSuperviseList(dklSupervise);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增督办信息
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertDklSupervise(DklSupervise dklSupervise)
|
||||
{
|
||||
dklSupervise.setCreateTime(DateUtils.getNowDate());
|
||||
return dklSuperviseMapper.insertDklSupervise(dklSupervise);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改督办信息
|
||||
*
|
||||
* @param dklSupervise 督办信息
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateDklSupervise(DklSupervise dklSupervise)
|
||||
{
|
||||
dklSupervise.setUpdateTime(DateUtils.getNowDate());
|
||||
return dklSuperviseMapper.updateDklSupervise(dklSupervise);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除督办信息
|
||||
*
|
||||
* @param ids 需要删除的督办信息主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteDklSuperviseByIds(Long[] ids)
|
||||
{
|
||||
return dklSuperviseMapper.deleteDklSuperviseByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除督办信息信息
|
||||
*
|
||||
* @param id 督办信息主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteDklSuperviseById(Long id)
|
||||
{
|
||||
return dklSuperviseMapper.deleteDklSuperviseById(id);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
<?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.DklSuperviseMapper">
|
||||
|
||||
<resultMap type="DklSupervise" id="DklSuperviseResult">
|
||||
<result property="id" column="id" />
|
||||
<result property="warningId" column="warning_id" />
|
||||
<result property="types" column="types" />
|
||||
<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="contents" column="contents" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="selectDklSuperviseVo">
|
||||
select ds.id, ds.warning_id, ds.types, ds.create_by, ds.create_time, ds.update_by, ds.update_time, ds.del_flag, ds.contents,dwi.warning_signs as warningName from dkl_supervise as ds
|
||||
LEFT JOIN dkl_warning_information dwi ON dwi.id = ds.warning_id
|
||||
</sql>
|
||||
|
||||
<select id="selectDklSuperviseList" parameterType="DklSupervise" resultMap="DklSuperviseResult">
|
||||
<include refid="selectDklSuperviseVo"/>
|
||||
<where>
|
||||
<if test="warningId != null and warningId != ''"> and ds.warning_id = #{warningId}</if>
|
||||
<if test="types != null and types != ''"> and ds.types = #{types}</if>
|
||||
<if test="contents != null "> and ds.contents = #{contents}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectDklSuperviseById" parameterType="Long" resultMap="DklSuperviseResult">
|
||||
<include refid="selectDklSuperviseVo"/>
|
||||
where ds.id = #{id}
|
||||
</select>
|
||||
|
||||
<insert id="insertDklSupervise" parameterType="DklSupervise">
|
||||
insert into dkl_supervise
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="warningId != null">warning_id,</if>
|
||||
<if test="types != null">types,</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="contents != null">contents,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id},</if>
|
||||
<if test="warningId != null">#{warningId},</if>
|
||||
<if test="types != null">#{types},</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="contents != null">#{contents},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="updateDklSupervise" parameterType="DklSupervise">
|
||||
update dkl_supervise
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
<if test="warningId != null">warning_id = #{warningId},</if>
|
||||
<if test="types != null">types = #{types},</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="contents != null">contents = #{contents},</if>
|
||||
</trim>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
<delete id="deleteDklSuperviseById" parameterType="Long">
|
||||
delete from dkl_supervise where id = #{id}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteDklSuperviseByIds" parameterType="String">
|
||||
delete from dkl_supervise where id in
|
||||
<foreach item="id" collection="array" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
||||
Loading…
Reference in New Issue