我正在尝试在junit mockito中测试一个方法,但是我无法模拟repo调用的方法(list projectbasicdetailslist=projectbasicdetailsrepository.findbyevprojectmaster(evprojectmaster);)。它示例化了一个不需要的空对象,测试应该创建一个projectbasicdetails的模拟列表,而不是创建一个空数组。甚至日志都指向未使用的mock。
代码:
@Test
public void testCreateServiceRequest() {
String evProjectId = String.valueOf(RandomGenerator.randomLong());
System.out.println("evProjectId is " + evProjectId);
EvProjectMaster evProjectMaster = new EvProjectMaster();
evProjectMaster.setEvProjectId(Long.valueOf(evProjectId));
System.out.println("evProjectMaster is " + evProjectMaster.getEvProjectId());
ProjectBasicDetails projectBasicDetails = new ProjectBasicDetails();
List<ProjectBasicDetails> projectBasicDetailsList = new ArrayList<>();
projectBasicDetails.setEvProjectMaster(evProjectMaster);
System.out.println(
"projectBasicDetails is " + projectBasicDetails.getEvProjectMaster().getEvProjectId());
projectBasicDetailsList.add(projectBasicDetails);
System.out.println("pbd list is : " + projectBasicDetailsList.size());
when(projectBasicDetailsRepository.findByEvProjectMaster(evProjectMaster))
.thenReturn(projectBasicDetailsList);
evProjectMaster.setProjectStartDate(RandomGenerator.randomDate().toLocalDate());
evProjectMaster.setProjectEndDate(RandomGenerator.randomDate().toLocalDate());
when(evProjectMasterRepository.findByEvProjectId(Long.valueOf(evProjectId)))
.thenReturn(evProjectMaster);
EquipmentEventDetails equipmentEventDetails =
EquipmentEventDetailsGenerator.randomEquipmentEventDetails();
equipmentEventDetails.setPlanEventStartDate(RandomGenerator.randomDate().toLocalDate());
List<EquipmentEventDetails> equipmentEventDetailsList = new ArrayList<>();
equipmentEventDetailsList.add(equipmentEventDetails);
when(equipmentEventDetailsRepository.findByEvProjectSourceId(evProjectId))
.thenReturn(equipmentEventDetailsList);
when(ofsEventsManager.checkSingleEvent(equipmentEventDetailsList)).thenReturn(true);
EquipmentDetail equipmentDetail = equipmentEventDetails.getEquipmentDetail();
when(equipmentDetailRepository
.findByEquipmentSysId(equipmentEventDetails.getEquipmentDetail().getEquipmentSysId()))
.thenReturn(equipmentDetail);
ServiceRequestModel srm = new ServiceRequestModel();
ServiceResponseModel serviceResponse = new ServiceResponseModel();
serviceResponse.setReturnStatus("S");
serviceResponse.setServiceRequestNo(RandomGenerator.randomString());
try {
when(charlieClient.triggerSrCreationInCharlie(srm)).thenReturn(serviceResponse);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<ServiceResponseModel> serviceResponseList = new ArrayList<>();
serviceResponseList.add(serviceResponse);
assertEquals(
charlieEvIntgManager.createServiceRequest(evProjectId).get(0).getServiceRequestNo(),
serviceResponse.getServiceRequestNo());
}
}
测试方法:
@Override
@Transactional
public List<ServiceResponseModel> createServiceRequest(String evProjectId) {
log.info("evProjectId is " + evProjectId);
boolean singleEvent = false;
String incidentRespondDate = null;
String eqpSysId = null;
EvProjectMaster evProjectMaster = new EvProjectMaster();
evProjectMaster.setEvProjectId(Long.valueOf(evProjectId));
List<ProjectBasicDetails> projectBasicDetailsList =
projectBasicDetailsRepository.findByEvProjectMaster(evProjectMaster);
evProjectMaster = evProjectMasterRepository.findByEvProjectId(Long.valueOf(evProjectId));
List<EquipmentEventDetails> equipmentEventDetailsList =
equipmentEventDetailsRepository.findByEvProjectSourceId(evProjectId);
singleEvent = ofsEventsManager.checkSingleEvent(equipmentEventDetailsList);
log.info("value of single event: {}", singleEvent);
log.info(" projectBasicDetails size " + projectBasicDetailsList.size());
运行单元测试的日志是:
evProjectId is 935603632
evProjectMaster is 935603632
projectBasicDetails is 935603632
pbd list is : 1
18:12:05.787 [main] INFO com.ge.seawolf.events.charlie.manager.impl.CharlieEvIntgManager - evProjectId is 935603632
18:12:05.801 [main] INFO com.ge.seawolf.events.charlie.manager.impl.CharlieEvIntgManager - value of single event: true
18:12:05.804 [main] INFO com.ge.seawolf.events.charlie.manager.impl.CharlieEvIntgManager - projectBasicDetails size 0
[MockitoHint] CharlieEvIntgManagerTest.testCreateServiceRequest (see javadoc for MockitoHint):
[MockitoHint] 1. Unused... -> at com.ge.seawolf.events.charlie.manager.impl.CharlieEvIntgManagerTest.testCreateServiceRequest(CharlieEvIntgManagerTest.java:755)
[MockitoHint] ...args ok? -> at com.ge.seawolf.events.charlie.manager.impl.CharlieEvIntgManager.createServiceRequest(CharlieEvIntgManager.java:666)
我做错什么了。任何线索都有帮助
1条答案
按热度按时间pxiryf3j1#
是否重写了evprojectmaster.class中的equals方法?
尝试将evprojectmaster改为mockito.any()在上面的代码中,它能工作吗?