代码之家  ›  专栏  ›  技术社区  ›  pise

org.junit.comparisonfailure:预期:<com.test.entity.vendorentity@3e60be48>但为:<null>

  •  0
  • pise  · 技术社区  · 6 年前

    我正在为服务层运行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> {
    }
    

    有人能告诉我密码有什么问题吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   bittu    6 年前

    您正在模拟对象vendorEntity的保存方法,但实际传递通过vendordto对象创建的其他对象。这两个都是不同的对象,我猜这会导致返回值为空。

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

    @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类似的对象(通过getvendor方法创建的对象),则测试用例将按预期工作。

    相似的物体意味着 equals 方法应返回 true 对于给定的对象。