I am running Junit test case for service layer but I am getting
org.junit.ComparisonFailure: expected:VendorEntity@3e60be48 but was:null
When vendorRepo.save(vendorEntity) method is called it returns null, I am not able to figure out why it is returning null. Below is my code.
@Autowired
private VendorSvc vendorSvc;
@MockBean
private VendorRepo vendorRepo;
@Test
public void testSaveVendorForm() {
VendorEntity vendorEntiy = getVendor();
Mockito.when(vendorRepo.save(vendorEntiy)).thenReturn(vendorEntiy);
// saveVendorForm return null
VendorEntity vendorEntity2 = vendorSvc.saveVendorForm(getVendorDto());
assertThat(vendorEntity2).isEqualTo(vendorEntiy);
}
After making some change in saveVendorForm which accept vendorEntity below code works but I don't want to pass entity class object to service layer as I want to create entity object in service layer and pass it to dao layer
@Test
public void testSaveVendorForm() {
VendorEntity vendorEntity = getVendor();
Mockito.when(vendorRepo.save(vendorEntity)).thenReturn(vendorEntity);
VendorEntity vendorEntity2 = vendorSvc.saveVendorForm(vendorEntity);
assertThat(vendorEntity2).isEqualTo(vendorEntity);
}
private VendorEntity getVendor() {
VendorEntity vendorEntity = new VendorEntity();
SocietyEntity societyEntity = new SocietyEntity();
societyEntity.setSocietyId(1L);
PincodeEntity pincodeEntity = new PincodeEntity();
pincodeEntity.setPincodeId(1L);
vendorEntity.setVendor("XYZ Cafe");
vendorEntity.setAddress("abc address");
vendorEntity.setEmailId("xyz@gmail.com");
vendorEntity.setContactNo1("123456");
vendorEntity.setContactNo2("123457");
vendorEntity.setSocietyId(societyEntity.getSocietyId());
vendorEntity.setPincodeId(pincodeEntity.getPincodeId());
vendorEntity.setWebsite("www.xyzabc.com");
vendorEntity.setCategoryId(2);
vendorEntity.setStatus(Constant.ACTIVE);
vendorEntity.setCreatedBy(1L);
vendorEntity.setCreatedDate(CommonUtil.getCurrentTimeStamp());
vendorEntity.setCreatedIp(Constant.DEFAULT_IP);
vendorEntity.setSocietyEntity(new SocietyEntity());
vendorEntity.setPincodeEntity(new PincodeEntity());
return vendorEntity;
}
@Override
public VendorEntity saveVendorForm(VendorDto vendorDto) {
VendorEntity vendorEntity = new VendorEntity();
// copy properties from (source,target)
BeanUtils.copyProperties(vendorDto,vendorEntity);
vendorEntity.setCreatedBy(vendorDto.getCreatedBy());
vendorEntity.setCreatedDate(vendorDto.getCreatedDate());
vendorEntity.setCreatedIp(vendorDto.getCreatedIp());
vendorEntity.setModifiedBy(vendorDto.getModifiedBy());
vendorEntity.setModifiedDate(vendorDto.getModifiedDate());
vendorEntity.setModifiedIp(vendorDto.getModifiedIp());
vendorEntity.setSocietyEntity(new SocietyEntity());
vendorEntity.setPincodeEntity(new PincodeEntity());
vendorEntity.setStatus(Constant.ACTIVE);
// below code returns null but works well when run in tomcat and form submitted through web browser
return vendorRepo.save(vendorEntity);
}
public interface VendorRepo extends JpaRepository<VendorEntity, Long> {
}
Can someone please tell me what is wrong in the code.