Springboot的Service层测试

当需要对service层某个方法进行单独调试,但使用postman或者swagger很麻烦时,可以采用单元测试。

package com.zh.sugarppig.service.frock;

import ...

/**
 * @author SugarPPig
 * @date 2020-08-28 11:00
 * @decription 工装审批接口
 */
public interface FrockApprovalService {

    /**
     * 审批列表
     *
     * @param condition 筛选条件
     * @return 分页显示的审批数据
     */
    PageInfo page(FrockApprovalCondition condition);

    /**
     * 审批操作
     *
     * @param frockAuditParam 审批操作内容
     * @return 审批结果
     */
    String pass(FrockAuditParam frockAuditParam);

}
package com.zh.sugarppig.service.frock.impl;

import ...

/**
 * @author SugarPPig
 * @date 2020-08-28 11:10
 * @decription 工装审批接口实现类
 */
@Service
public class FrockApprovalServiceImpl implements FrockApprovalService {

    @Autowired
    private FrockApplyDao frockApplyDao;

    @Autowired
    private WfRuntimeService wfRuntimeService;

    @Override
    public PageInfo page(FrockApprovalCondition condition) {
        // ...
    }

    @Override
    public String pass(FrockAuditParam frockAuditParam) {
        Long taskId = frockAuditParam.getTaskId();
        if (taskId == null) throw new GeneralException("参数错误");

        TaskAudit taskAudit = new TaskAudit();
        taskAudit.setRemark(frockAuditParam.getRemark());
        taskAudit.setTaskStatus(frockAuditParam.getStatus());
        taskAudit.setTaskId(taskId);
        FrockApply frockApply = frockApplyDao.getApplyInfoById(frockAuditParam.getId());
        wfRuntimeService.doTask(taskAudit, frockApply, (flowData, flowInstanceStatus) -> {
            if (FlowInstanceStatus.PASS == flowInstanceStatus) {
                //审批通过
                frockApply.setStatus(FrockStatusEnum.PASS.getKey());
            } else if (FlowInstanceStatus.REJECT == flowInstanceStatus) {
                //审批驳回
                frockApply.setStatus(FrockStatusEnum.REJECT.getKey());
            }
            frockApplyDao.updateByPrimaryKeySelective(frockApply);
        });
        return "成功";
    }
}

点击鼠标右键,选择“生成”

Springboot的Service层测试_第1张图片

选择“Test...”

Springboot的Service层测试_第2张图片

勾选"pass"方法,点击确定

Springboot的Service层测试_第3张图片

配置好注解,添加上测试数据,运行即可!

package com.zh.sugarppig.service.frock.impl;

import ...

/**
 * @author SugarPPig
 * @date 2020-08-28 11:20
 * @decription 工装审批流程测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class FrockApprovalServiceImplTest {

    @Resource
    FrockApplyDao frockApplyDao;

    @Resource
    WfRuntimeService wfRuntimeService;

    @Test
    @Transactional
    @Rollback
    public void pass() {
        // 组装测试数据
        FrockAuditParam frockAuditParam = new FrockAuditParam();
        frockAuditParam.setId(1);
        frockAuditParam.setTaskId(1L);
        frockAuditParam.setStatus(1);
        frockAuditParam.setRemark("审批流程测试");

        // 执行审批流程
        TaskAudit taskAudit = new TaskAudit();
        taskAudit.setRemark(frockAuditParam.getRemark());
        taskAudit.setTaskStatus(frockAuditParam.getStatus());
        taskAudit.setTaskId(frockAuditParam.getTaskId());
        FrockApply frockApply = frockApplyDao.getApplyInfoById(frockAuditParam.getId());
        wfRuntimeService.doTask(taskAudit, frockApply, (flowData, flowInstanceStatus) -> {
            if (FlowInstanceStatus.PASS == flowInstanceStatus) {
                //审批通过
                frockApply.setStatus(FrockStatusEnum.PASS.getKey());
            } else if (FlowInstanceStatus.REJECT == flowInstanceStatus) {
                //审批驳回
                frockApply.setStatus(FrockStatusEnum.REJECT.getKey());
            }
            frockApplyDao.updateByPrimaryKeySelective(frockApply);
        });
    }
}

 

你可能感兴趣的:(java,单元测试)