在尝试使用JPA创建用于删除的junit测试用例时,我遇到了一些问题。在我的服务类中,我使用的是 JPA 提供的缺省函数:public void delete(Long id) { log.debug("Request to delete Enroll : {}", id); enrollRepository.deleteById(id);}然后我的单元测试,我试图模拟一个对象,然后执行删除:@Testpublic void testDeleteEnrollExpectSuccess() { Enroll enroll = prepareEnrollWithBedAllocation(); enrollService.delete(enroll.getId()); verify(enrollRepository, times(1)).delete(enroll);}我打算像这样嘲笑注册对象:private Enroll prepareEnrollWithBedAllocation() { LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC")); Enroll enroll = new Enroll(); enroll.setId(new Long(1)); enroll.seteDate(zonedDateTime); enroll.setEbStartdt(zonedDateTime); enroll.setEbEnddt(zonedDateTime); enroll.setBed(prepareBed()); enroll.setPatient(preparePatient()); enroll.setDoc(prepareDoc()); enroll.seteCondition("Normal"); return enroll;}但是,我收到这些错误消息,并且我的测试用例失败:Wanted but not invoked:enrollRepository.delete( Enroll{id=1, eCondition='Normal', eRemarks='null', ebStartdt='2019-04-09T16:40:32.703Z[UTC]', ebEnddt='2019-04-09T16:40:32.703Z[UTC]', eDate='2019-04-09T16:40:32.703Z[UTC]'});-> at com.team.generated.service.EnrollServiceTest.testDeleteEnrollExpectSuccess(EnrollServiceTest.java:61)However, there was exactly 1 interaction with this mock:enrollRepository.deleteById(1L);-> at com.team.generated.service.EnrollService.delete(EnrollService.java:74)有什么想法吗?谢谢!
1 回答
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
该错误本身就说明了:您正在验证该方法:delete(Long id)
verify(enrollRepository, times(1)).delete(enroll);
但是,您的服务对象调用方法:deleteById()
enrollRepository.deleteById(id);
我想你想这样做:
@Testpublic void testDeleteEnrollExpectSuccess() { Enroll enroll = prepareEnrollWithBedAllocation(); enrollService.delete(enroll.getId()); verify(enrollRepository, times(1)).deleteById(enroll.getId()); }
添加回答
举报
0/150
提交
取消