Hi,
I tried to work around this codePro and i found that when i generate Junit test cases (with mockObject)the code Pro is not expecting the service method call, Like as below:
The actual method is this:
public void editAction ( )
{
this.custDistItem = this.distributorItemService
.editAndUpdate( this.selectedRow );
this.setNewDistributorId( this.custDistItem
.getCustDistributor().getId() );
}
where in distributorItemService is the service layer, which actually the code Pro should expect, but is happening is the code pro is neither expecting nor doing anything due to which the "this.custDistItem" that is calling this service is setting this value to null & so since this variable is used to get the ID which is throwing a NullPointerException.Find below the test case methods created by code pro for this above method:
public void testEditAction_1()
throws Exception
{
DistributorItemBean fixture = new DistributorItemBean();
fixture.setDistributorService(EasyMock.createNiceMock(DistributorService.class));
CustItem custItem = new CustItem();
CustDistributor custDistributor = new CustDistributor();
custDistributor.setId(new Long(5L));
custItem.setCustDistributor(custDistributor);
fixture.setCustDistItem(custItem);
fixture.setDeleteItemFlag(true);
fixture.setDistributorItemService(EasyMock.createNiceMock(DistributorItemService.class));
fixture.setStoreWeightUOM(new HashMap<String, String>());
fixture.setItemDesc("");
fixture.setStoreAllUOM(new HashMap<String, String>());
fixture.setSaveUpdateButton("");
fixture.setAddEditDistributorList(new ArrayList<SelectItem>());
fixture.setDistCode(new Long(0L));
fixture.setUserProfileDTO(new UserProfileDTO());
fixture.setDisableUpdate(false);
fixture.setAddEditFlag(true);
fixture.setDeleteRow(new Long(0L));
fixture.setRmUOMService(EasyMock.createNiceMock(RMUOMService.class));
fixture.setCustDistributorList(new ArrayList<CustDistributor>());
fixture.setDistributorsList(new ArrayList<SelectItem>());
fixture.setSelectedRow(new Long(0L));
fixture.setItemList(new ArrayList<CustItem>());
fixture.setAddEditLabel("");
fixture.setNewDistributorId(new Long(0L));
fixture.setItemNum("");
fixture.setCheckCode(true);
fixture.editAction();
// add additional test code here
// An unexpected exception was thrown while executing this test:
// java.lang.NullPointerException
// at com.gfs.ebiz.rm.app.ui.controller.DistributorItemBean.editAction(DistributorItemBean.java:348)
}
Can anyone tell me how can i achieve this , is it that i am missing any settings or codePro wont do "expect", if so then how code Pro is generating testcases for others. I am trying to achieve something like this below, not the exact but something of this kind :
public void testEditAction ( )
{
this.itemBean.setSelectedRow( 7L );
custDistributor.setId( 7L );
custItem.setCustDistributor( custDistributor );
custItem.setBreakInd( true );
custItem.setFullcaseUomCode( "CASE" );
custItem.setLesscaseUomCode( "CASE" );
custItem.setCatchWeightInd( true );
itemBean.setDisableUpdate( true );
expect( this.distributorItemService.editAndUpdate( 7L ) ).andReturn(
custItem );
replay( this.distributorItemService );
this.itemBean.editAction();
verify( this.distributorItemService );
// assertFalse( this.itemBean.isFullUnitFlag() );
}