Issue
I am trying to test a method in Junit Mockito, but I am not able to mock the method for a Repo call (List projectBasicDetailsList = projectBasicDetailsRepository.findByEvProjectMaster(evProjectMaster);) . It is instantiating a blank object which is not expected, the test should create a mock List of ProjectBasicDetails , Instead it is creating a null array. Even the log is pointing of unused mock.
Code :
@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());
}
}
Method to test:
@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());
The log which I am getting on running the unit test is :
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)
What am I doing wrong here. Any clue will help
Solution
Have you overridden the equals method in EvProjectMaster.class?
when(projectBasicDetailsRepository.findByEvProjectMaster(evProjectMaster)).thenReturn(projectBasicDetailsList);
Try to change evProjectMaster to Mockito.any() in upper code, does it works?
Answered By - Grigorii Riabov
Answer Checked By - David Marino (JavaFixing Volunteer)