老师测试用例出错
package com.imooc.oa.service; import com.imooc.oa.entity.LeaveForm; import org.junit.Test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import static org.junit.Assert.*; public class LeaveFormServiceTest { LeaveFormService leaveFormService = new LeaveFormService(); @Test public void createLeaveForm() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH"); LeaveForm form = new LeaveForm(); form.setEmployeeId(8l); form.setStartTime(sdf.parse("2020032608")); form.setEndTime(sdf.parse("2020040118")); form.setFormType(1); form.setReason("市场部员工请假单(72小时以上)"); form.setCreateTime(new Date()); LeaveForm savedForm = leaveFormService.createLeaveForm(form); System.out.println(savedForm.getFormId()); } }
package com.imooc.oa.service; import com.imooc.oa.entity.Employee; import com.imooc.oa.entity.LeaveForm; import com.imooc.oa.entity.ProcessFlow; import com.imooc.oa.mapper.EmployeeMapper; import com.imooc.oa.mapper.LeaveFormMapper; import com.imooc.oa.mapper.NoticeMapper; import com.imooc.oa.mapper.ProcessFlowMapper; import com.imooc.oa.utils.MybatisUtils; import java.text.SimpleDateFormat; import java.util.Date; public class LeaveFormService { private EmployeeService employeeService = new EmployeeService(); public LeaveForm createLeaveForm(LeaveForm form) { /** * 创建请假单 * @param form 前端输入的请假单数据 * @return 持久化后的请假单对象 */ LeaveForm f = (LeaveForm) MybatisUtils.executeUpdate(sqlSession -> { //1.持久化form表单数据,8级以下员工表单状态为processing,8级(总经理)状态为approved EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class); Employee employee = employeeMapper.selectById(form.getEmployeeId()); if (employee.getLevel() == 8) { form.setState("approved"); } else { form.setState("processing"); } LeaveFormMapper leaveFormMapper = sqlSession.getMapper(LeaveFormMapper.class); leaveFormMapper.insert(form); NoticeMapper noticeMapper = sqlSession.getMapper(NoticeMapper.class); //2.增加第一条流程数据,说明表单已提交,状态为complete ProcessFlowMapper processFlowMapper = sqlSession.getMapper(ProcessFlowMapper.class); ProcessFlow flow1 = new ProcessFlow(); flow1.setFormId(form.getFormId()); flow1.setOperatorId(employee.getEmployeeId()); flow1.setAction("apply"); flow1.setCreateTime(new Date()); flow1.setOrderNo(1); flow1.setState("complete"); flow1.setIsLast(0); ProcessFlowMapper.insert(flow1); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH时"); //3.分情况创建其余流程数据 //3.1 7级以下员工,生成部门经理审批任务,请假时间大于等于72小时,还需生成总经理审批任务 if (employee.getLevel() < 7) { Employee dmanger = employeeService.selectLeader(employee.getEmployeeId()); ProcessFlow flow2 = new ProcessFlow(); flow2.setFormId(form.getFormId()); flow2.setOperatorId(dmanger.getEmployeeId()); flow2.setAction("audit"); flow2.setCreateTime(new Date()); flow2.setState("process"); long diff = form.getEndTime().getTime() - form.getStartTime().getTime(); float hours = diff / (1000 * 60 * 60) * 1f; if (hours >= 72) { flow2.setIsLast(0); ProcessFlowMapper.insert(flow2); Employee manager = employeeService.selectLeader(dmanger.getEmployeeId()); ProcessFlow flow3 = new ProcessFlow(); flow3.setFormId(form.getFormId()); flow3.setOperatorId(manager.getEmployeeId()); flow3.setAction("audit"); flow3.setCreateTime(new Date()); flow3.setState("ready"); flow3.setOrderNo(3); flow3.setIsLast(1); ProcessFlowMapper.insert(flow3); } else { flow2.setIsLast(1); ProcessFlowMapper.insert(flow2); } } else if (employee.getLevel() == 7) { //3.2 7级员工,仅生成总经理审批业务 Employee manager = employeeService.selectLeader(employee.getEmployeeId()); ProcessFlow flow2 = new ProcessFlow(); flow2.setFormId(form.getFormId()); flow2.setOperatorId(manager.getEmployeeId()); flow2.setAction("audit"); flow2.setCreateTime(new Date()); flow2.setState("process"); flow2.setOrderNo(2); flow2.setIsLast(1); ProcessFlowMapper.insert(flow2); } else if (employee.getLevel() == 8) { //3.3 8级员工。生成总经理审批任务,系统自动通过 ProcessFlow flow2 = new ProcessFlow(); flow2.setFormId(form.getFormId()); flow2.setOperatorId(employee.getEmployeeId()); flow2.setAction("audit"); flow2.setResult("approved"); flow2.setReason("自动通过"); flow2.setCreateTime(new Date()); flow2.setAuditTime(new Date()); flow2.setState("complete"); flow2.setOrderNo(2); flow2.setIsLast(1); ProcessFlowMapper.insert(flow2); } return null; }); return f; } }
6
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕小蓝
2024-09-06 17:47:09
同学你好,老师没有理解同学说的出错了是指出什么错了。
最近同学提问都是这种形式,非常不利于老师对同学问题的排查。提问时应当将问题描述清楚,例如当出现错误时,应当描述一下在执行什么操作的时候,预期什么效果,但是实际出现什么效果。或者有报错的时候,应当提供报错信息。而不是直接将一大片代码复制提供到问答区,由老师去猜测出什么错,再去挨个测试。这样达不到效果的主要原因是,老师这边是没有同学的项目的,只能配合源码去测试,之前多个问答已经出现过了,在老师这里,代码并不会有错,因为出错的位置不在提供的代码中,所以描述清楚问题更显得尤为关键。
在同学的本个问答中,测试代码本身没有问题,但是LeaveFormService中同学使用了NoticeMapper,由于没有注释、没有问题说明、没有报错截图、没有报错信息,老师是不能理解这里为什么要使用NoticeMapper。并且同学代码中,ProcessFlowMapper的insert方法使用的是静态调用方式,也是因为没有任何描述老师是不理解为什么这么调用的,正常都是使用小写的processFlowMapper调用成员方法。
所以,建议同学以后提问时,一定要描述清楚问题,并且将所需的代码、截图、报错信息一并提供。
祝学习愉快~
Java工程师 2024版
- 参与学习 2020 人
- 提交作业 1249 份
- 解答问题 1202 个
2024重磅革新,超百小时内容豪华升级,加速提升高级技能与高薪就业竞争力 课程紧贴企业最新人才需求,历经7年持续迭代,帮助万名学子入行转行 从零起点到高阶实战,学习路径稳健顺滑,成就从小白到工程师高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星