在嘗試使用JPA創(chuàng)建用于刪除的junit測試用例時,我遇到了一些問題。在我的服務(wù)類中,我使用的是 JPA 提供的缺省函數(shù):public void delete(Long id) { log.debug("Request to delete Enroll : {}", id); enrollRepository.deleteById(id);}然后我的單元測試,我試圖模擬一個對象,然后執(zhí)行刪除:@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;}但是,我收到這些錯誤消息,并且我的測試用例失?。篧anted 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貢獻(xiàn)1725條經(jīng)驗 獲得超8個贊
該錯誤本身就說明了:您正在驗證該方法:delete(Long id)
verify(enrollRepository, times(1)).delete(enroll);
但是,您的服務(wù)對象調(diào)用方法:deleteById()
enrollRepository.deleteById(id);
我想你想這樣做:
@Testpublic void testDeleteEnrollExpectSuccess() { Enroll enroll = prepareEnrollWithBedAllocation(); enrollService.delete(enroll.getId()); verify(enrollRepository, times(1)).deleteById(enroll.getId()); }
添加回答
舉報
0/150
提交
取消