朱尼特与莫基托



我正在用Mockito测试一个Spring impl类。

有一个对返回 void 的方法的调用。

但是方法(addCruiseMessages)返回NullPointerException因为它调用了属性文件。

如何成功测试?

英普尔类:

@Path(CruisePricingConstants.SAVE_CRUISE_RESTRICTION)
public Integer saveCruiseRestrictionDetails (List<CruisePricingCruiseDetailsVO> cruiseDetailsVOs){
    List<MessageVO> messageVOs = new ArrayList<MessageVO>();

    MessageVO message = null;
   for(CruisePricingCruiseDetailsVO  cruisePricingCruiseDetailsVO : cruiseDetailsVOs)
   {
       int listLength = cruisePricingCruiseDetailsVO.getViewImpactedCruisesDefinintionVOs().size();
       if(listLength > CruisePricingConstants.ONE)
       {
           Set<CruisePricingViewImpactedCruisesDefinintionVO> set = new HashSet<CruisePricingViewImpactedCruisesDefinintionVO>()  ;
           set.addAll(cruisePricingCruiseDetailsVO.getViewImpactedCruisesDefinintionVOs());
           if(set.size()<listLength)
           {
               message = new MessageVO("msg.searchAndListViewImpactedCruisesWidget.duplicateRecordExist", SproutUtil.getMessage("msg.searchAndListViewImpactedCruisesWidget.invalidCombination"), ErrorSeverity.ERROR);
               messageVOs.add(message);
               WebExceptionUtil.addMessagesToResponse(messageVOs);
               return CruisePricingConstants.ONE;
           }
       }
   }
    LOGGER.debug("CruisePricingViewImpactedCruisesServiceImpl :: saveCruiseRestrictionDetails :: Entry");
    Integer rateID = viewImpactedCruisesComponent.saveCruiseRestrictionDetails(cruiseDetailsVOs);
    if(rateID==CruisePricingConstants.ONE){   
        addCruiseMessages();
        }
    LOGGER.debug("CruisePricingViewImpactedCruisesServiceImpl :: saveCruiseRestrictionDetails :: Exit");
    return rateID;
}

测试类。

   @Test
   public void saveCruiseRestrictionDetails(){    
          CruisePricingCruiseDetailsVO cruisePricingCruiseDetailsVO = MockFactory.populateCruiseRequestVO();  
          List<CruisePricingCruiseDetailsVO> cruisePricingCruiseDetailsVOs=new ArrayList<CruisePricingCruiseDetailsVO>();
          CruisePricingViewImpactedCruisesDefinintionVO cruisePricingViewImpactedCruisesDefinintionVO=MockFactory.populateImpactedVO();
          List<CruisePricingViewImpactedCruisesDefinintionVO> cruisePricingViewImpactedCruisesDefinintionVOs=new ArrayList<CruisePricingViewImpactedCruisesDefinintionVO>();
          cruisePricingViewImpactedCruisesDefinintionVOs.add(cruisePricingViewImpactedCruisesDefinintionVO);
          cruisePricingCruiseDetailsVO.setViewImpactedCruisesDefinintionVOs(cruisePricingViewImpactedCruisesDefinintionVOs);
          cruisePricingCruiseDetailsVOs.add(cruisePricingCruiseDetailsVO);
            when(cruisePricingViewImpactedCruisesComponent.saveCruiseRestrictionDetails(cruisePricingCruiseDetailsVOs)).thenReturn(1);
          CruisePricingViewImpactedCruisesServiceImpl cimpl=mock(CruisePricingViewImpactedCruisesServiceImpl.class);
          CruisePricingViewImpactedCruisesServiceImpl cp=new CruisePricingViewImpactedCruisesServiceImpl();
          Mockito.stubVoid(cimpl).toReturn().on().addCruiseMessages();
          cruisePricingViewImpactedCruisesServiceImpl.saveCruiseRestrictionDetails(cruisePricingCruiseDetailsVOs);
         assertEquals(new Integer(1),cruisePricingViewImpactedCruisesServiceImpl.saveCruiseRestrictionDetails(cruisePricingCruiseDetailsVOs));

   }`

如果SproutUtil.getMessage是导致问题的方法,那么您必须:

1)提取到吸气剂方法:

public String getMessage(String message) {
    return SproutUtil.getMessage(message);
}

2) 更改实现,使其调用 getter 而不是显式调用静态实用程序:

message = new MessageVO("msg.searchAndListViewImpactedCruisesWidget.duplicateRecordExist"
,getMessage("msg.searchAndListViewImpactedCruisesWidget.invalidCombination"), ErrorSeverity.ERROR);

3)用间谍注释巡航定价视图受影响的游轮组件(因为我相信您想要实际调用的一些方法):

@Spy
private CruisePricingViewImpactedCruisesComponent cmp;

4)模拟吸气者

when(cmp.getMessage(Mockito.any(String.class))).thenReturn("anyString");

试试吧。

相关内容

  • 没有找到相关文章

最新更新