org.junit.ComparisonFailure:预期:<com.test.entity.VendorEntity@3e60be48> 但实际是:<null>

org.junit.ComparisonFailure: expected:<com.test.entity.VendorEntity@3e60be48> but was:<null>

我是 运行 服务层的 Junit 测试用例,但我得到

org.junit.ComparisonFailure: expected:VendorEntity@3e60be48 but was:null

当调用 vendorRepo.save(vendorEntity) 方法时 returns null,我无法弄清楚为什么它返回 null。下面是我的代码。

@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> {
}

谁能告诉我代码中有什么问题。

您正在模拟对象 vendorEntity 的保存方法,但实际上传递了通过 VendorDto 对象创建的不同对象。两者都是不同的对象,我猜这会在 return.

中导致 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 方法应该 return true 对于给定的对象。