0

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.

pise
  • 849
  • 6
  • 24
  • 51

1 Answers1

0

You are mocking save method for object vendorEntity but actaully passing a different object created via VendorDto object. Both are different object I guess which causing null in return.

Follow my comment on your test case(did not make any change except comments).

@Test
public void testSaveVendorForm() {
     VendorEntity vendorEntiy = getVendor();
     //Mocking the verndorRepo.save to return vendorEntiy when save is called with vendorEntiy
     Mockito.when(vendorRepo.save(vendorEntiy)).thenReturn(vendorEntiy);
     // saveVendorForm return null 
     // Actually passed a different object which may not be equal to vendorEntiy
     VendorEntity vendorEntity2 = vendorSvc.saveVendorForm(getVendorDto());
     assertThat(vendorEntity2).isEqualTo(vendorEntiy);
}

saveVendorForm may not be generating the exact VendorEntity object that we configured in mocking.

So if you make sure that getVendorDto() to VendorEntity trasnformation generates object similar to vendorEntity(the one which is getting created via getVendor method) then your test case would work as expected.

Similar objects means equals method should return true for given objects.

bittu
  • 786
  • 7
  • 14
  • will valid and post the result – pise Nov 26 '18 at 07:52
  • Yup let me know if any issues. – bittu Nov 26 '18 at 12:05
  • I found that since 2 different VendorEntity object is passed even though both have same value saveVendorForm return null. If I make a change in saveVendorForm(vedorEntity) which accept vendorEntity instead of vendorDto and pass the same vendorEntity then junit runs successfull. I have added code with is working – pise Nov 28 '18 at 08:22
  • 1
    I doubt something is wrong between conversion from DTO to VendorEntity and Does VendorEntity/SocietyEntity/PincodeEntity has equals method implemented ? as we are creating the new object for SocietyEntity/PincodeEntity while doing the conversion. – bittu Nov 28 '18 at 08:47
  • It finally worked after implement equals and hashcode Using the entity identifier and the test passed successfully – pise Nov 29 '18 at 15:43
  • Right. This is what I was expecting. Good that finally you got it sorted – bittu Nov 29 '18 at 18:20