我正在为服务层运行Junit测试用例,但是我得到了
org.junit.ComparisonFailure:预期:VendorEntity @ 3e60be48,但为:空
当调用vendorRepo.save(vendorEntity)方法时,它返回空值,我无法弄清楚为什么它返回空值。下面是我的代码。
@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);
}
在对saveVendorForm进行一些更改后,下面的代码可以接受vendorEntity,但是我不想将实体类对象传递给服务层,因为我想在服务层中创建实体对象并将其传递给dao层
@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> {
}
有人可以告诉我代码中有什么问题吗
答案 0 :(得分:0)
您正在模拟对象vendorEntity的保存方法,但是实际上传递了通过VendorDto对象创建的另一个对象。我猜这两个都是不同的对象,导致返回null。
按照我对您的测试用例的评论(除了评论,没有做任何其他更改)。
@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
可能没有生成我们在模拟中配置的确切VendorEntity对象。
因此,如果确保getVendorDto()
到VendorEntity转换生成的对象类似于vendorEntity(通过getVendor方法创建的对象),则您的测试用例将按预期工作。
相似的对象意味着equals
方法应为给定的对象返回true
。