我正在尝试为下面的代码编写单元测试,在这里我实例化一个bean并将其传递给类的其他私有方法以进行进一步处理,然后再执行main方法:
public OverviewTabViewBean build(Listing listing) {
VehicleDetailListingBean vehicleDetailListingBean = new VehicleDetailListingBean();
applyVehicleDetailListingRules(listing, vehicleDetailListingBean);
OverviewTabViewBean overviewTabViewBean = new OverviewTabViewBean();
if (vehicleDetailListingBean != null) {
overviewTabViewBean.setMake(vehicleDetailListingBean.getMake());
overviewTabViewBean.setModel(vehicleDetailListingBean.getModel());
overviewTabViewBean.setAtCarId(vehicleDetailListingBean.getAtCarId());
..
}
return overviewTabViewBean;
private void applyCommonListingRules(Listing listing, VehicleDetailListingBean vehicleDetailListingBean) {
rulesEngineService.applyRules(listing, vehicleDetailListingBean, vehicleDetailRules.getCommonListingRules());
}
测试看起来像这样:
@Captor
ArgumentCaptor<OverviewTabViewBean> overviewTabViewBean;
@Captor
ArgumentCaptor<VehicleDetailListingBean> vehicleDetailListingBean;
@Mock
private RulesEngineService rulesEngineService;
@Mock
private VehicleDetailRules vehicleDetailRules;
@Mock
private VehicleReferenceService vehicleReferenceService;
@BeforeMethod
public void setUp() {
MockitoAnnotations.initMocks(this);
}
/**
* build
*/
@Test
public void build() {
Listing listing = new Listing();
listing.setListingId(111L);
listing.setListingType(ListingType.NEW);
List<Rule<Listing, ListingBean>> rules1 = new ArrayList<Rule<Listing, ListingBean>>();
List<Rule<Listing, ListingBean>> rules2 = new ArrayList<Rule<Listing, ListingBean>>();
doReturn(rules1).when(vehicleDetailRules).getCommonListingRules();
doReturn(rules2).when(vehicleDetailRules).getDetailListingRules();
overviewTabViewBeanBuilder.build(listing);
verify(rulesEngineService, times(2)).applyRules(anyObject(), vehicleDetailListingBean.capture(), anyList());
.....
}
vehicleDetailListingBean 返回null并且不进入if块。我在这里做错了什么?