【问题标题】:org.junit.ComparisonFailure: expected:<com.test.entity.VendorEntity@3e60be48> but was:<null>org.junit.ComparisonFailure:预期:<com.test.entity.VendorEntity@3e60be48> 但原为:<null>
【发布时间】:2018-11-25 10:03:19
【问题描述】:

我正在为服务层运行 Junit 测试用例,但我得到了

org.junit.ComparisonFailure:预期:VendorEntity@3e60be48 但为:null

当调用 vendorRepo.save(vendorEntity) 方法时它返回 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);
}

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

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

【问题讨论】:

  • 如何在您的测试类中声明 vendorRepo ?你能附上完整的测试类吗?
  • @bittu 我已经为 vendorRepo 添加了代码

标签: spring-boot junit5 spring-boot-test


【解决方案1】:

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

按照我对您的测试用例的评论(除了 cmets 没有做任何更改)。

@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

【讨论】:

  • 将生效并发布结果
  • 是的,如果有任何问题,请告诉我。
  • 我发现,由于传递了 2 个不同的 VendorEntity 对象,即使它们具有相同的值 saveVendorForm 返回 null。如果我在 saveVendorForm(vedorEntity) 中进行更改,它接受 vendorEntity 而不是 vendorDto 并传递相同的 vendorEntity,那么 junit 运行成功。我添加了正在运行的代码
  • 我怀疑从 DTO 到 VendorEntity 的转换和 VendorEntity/SocietyEntity/PincodeEntity 是否实现了 equals 方法之间有什么问题?因为我们在进行转换时正在为 SocietyEntity/PincodeEntity 创建新对象。
  • 使用实体标识符实现equals和hashcode后终于成功了,测试成功
猜你喜欢
  • 1970-01-01
  • 2019-02-26
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 2018-07-05
  • 2019-04-13
相关资源
最近更新 更多