Code Pro AnalatiX not "Expecting" the mock object methods

CodePro AnalytiX (Audit & Test), includes over 1000 audit rules and metrics, JUnit test generation & editing, code coverage, and duplicate code analysis

Moderators: gnebling, jwren, Eric Clayberg, Dan Rubel, Brian Wilkerson, dcarew

Code Pro AnalatiX not "Expecting" the mock object methods

Postby pavi4u143 » Fri Apr 24, 2009 1:26 am

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() );
}
pavi4u143
 
Posts: 7
Joined: Mon Apr 20, 2009 3:38 am

Re: Code Pro AnalatiX not "Expecting" the mock object methods

Postby dcarew » Tue Apr 28, 2009 7:03 am

Hi Pavi, sorry for the delay in responding to your post. Our normal customer support channel is cpsws-support (at) instantiations.com - if you send emails there we have a pretty good (< 24 hour) response time.

In response to your question, CodePro does not fill in the expectations of the mock objects. It generates the skeleton code but leaves the mock object expectations for the user to fill in. Filling in the expectations automatically is a pretty tricky proposition. It requires some sophisticated inter-procedural data flow analysis. It's something we've considered, and looked in to, but have not yet implemented.

As I said in a previous post, because we're mocking a field of a parameter into the method under test, we're not generating the full create mock, set expectations, replay, and verify mock object code. We've made the (often but not always true) assumption that this mock object is not semantically part of the test itself. Your best bet is to use CodePro to generate the tests and then manually add in the expectations for the mock objects (as you've outlined above). This is probably not what you want to hear, but what you'll have to do for now.

Sorry again it took so long to respond to you. Cheers,

Devon Carew
Instantiations, Inc.
dcarew
 
Posts: 2
Joined: Mon Apr 20, 2009 9:51 am

Re: Code Pro AnalatiX not "Expecting" the mock object methods

Postby pavi4u143 » Wed Apr 29, 2009 4:36 am

Hi carew,
I am extremely glad to see your reply atlast, but in the same way unhappy that i need to manually need to expect the methods, I am sorry to say but then there is no reason in saying that its a "Automated test case code generation tool".
Going further as what you have mentioned like putting manually my own expect methods, i have tried this also but even then whatever the expect is returning through the '.andReturn()' still the value is getting null. Its means whatever i am trying to expect its not either expecting & so not returning anything.
This is the code for what am trying to generate the Junit test case:
public void editAction ( )
{
this.custDistItem = this.distributorItemService
.editAndUpdate( this.selectedRow );
this.setNewDistributorId( this.custDistItem
.getCustDistributor().getId() );
}
where in you can see whatever the "distributorItemService" service class "editAndUpdate" method returns is stored in the "custDistItem" DTO and the same DTO "CustDistributor().getId()" is used, since the "custDistItem" is null i am getting the nullPointerException.
Please refer the code below generated by the tool:
/**
* Run the void editAction() method test.
*
* @generatedBy CodePro at 4/28/09 12:24 PM
*/
public void testEditAction_1()
throws Exception
{
try {
DistributorItemBean fixture = new DistributorItemBean();
CustItem custItem = new CustItem();
CustDistributor custDistributor = new CustDistributor();
custDistributor.setId(new Long(0L));
custItem.setCustDistributor(custDistributor);
fixture.setCustDistItem(custItem);
fixture.setUserProfileDTO(new UserProfileDTO());
fixture.setItemNum("");
fixture.setCustDistributorList(new ArrayList<CustDistributor>());
fixture.setSaveUpdateButton("");
fixture.setRmUOMService(new SpringUOMService());
fixture.setAddEditFlag(true);
fixture.setDistributorItemService(new SpringDistributorItemService());
fixture.setAddEditLabel("");
fixture.setDeleteRow(new Long(0L));
fixture.setNewDistributorId(new Long(0L));
fixture.setDistributorsList(new ArrayList<SelectItem>());
fixture.setStoreWeightUOM(new HashMap<String, String>());
fixture.setStoreAllUOM(new HashMap<String, String>());
fixture.setItemList(new ArrayList<CustItem>());
fixture.setItemDesc("");
fixture.setSelectedRow(new Long(0L));
fixture.setDeleteItemFlag(true);
fixture.setDistCode(new Long(0L));
fixture.setCheckCode(true);
fixture.setAddEditDistributorList(new ArrayList<SelectItem>());
fixture.setDisableUpdate(false);
fixture.setDistributorService(new SpringDistributorService());

fixture.editAction();

// add additional test code here
fail("The exception java.lang.NullPointerException should have been thrown.");
} catch (java.lang.NullPointerException exception) {
// The test succeeded by throwing the expected exception
}
}
Find below the code where am expecting the service call:
public void testSetDistributorItemService_1()
throws Exception
{
DistributorItemBean fixture = new DistributorItemBean();
fixture.setDeleteItemFlag(true);
fixture.setAddEditFlag(true);
fixture.setSelectedRow(new Long(0L));
fixture.setStoreAllUOM(new HashMap<String, String>());
fixture.setItemNum("");
fixture.setStoreWeightUOM(new HashMap<String, String>());
fixture.setAddEditLabel("");
fixture.setCustDistItem(new CustItem());
fixture.setNewDistributorId(new Long(0L));
fixture.setCustDistributorList(new ArrayList<CustDistributor>());
fixture.setAddEditDistributorList(new ArrayList<SelectItem>());
fixture.setSaveUpdateButton("");
fixture.setDistributorItemService(EasyMock.createNiceMock(DistributorItemService.class));
fixture.setRmUOMService(EasyMock.createNiceMock(RMUOMService.class));
fixture.setCheckCode(true);
fixture.setDistributorsList(new ArrayList<SelectItem>());
fixture.setItemList(new ArrayList<CustItem>());
fixture.setDistributorService(EasyMock.createNiceMock(DistributorService.class));
fixture.setDistCode(new Long(0L));
fixture.setItemDesc("");
fixture.setDeleteRow(new Long(0L));
fixture.setUserProfileDTO(new UserProfileDTO());
fixture.setDisableUpdate(true);
DistributorItemService distributorItemService = EasyMock.createMock(DistributorItemService.class);
fixture.setDistributorItemService(distributorItemService);
// add mock object expectations here
CustItem custItem12 = new CustItem();
CustDistributor custDistributor12 = new CustDistributor();
custDistributor12.setId( 23L );
custItem12.setCustDistributor( custDistributor12 );
EasyMock.expect( distributorItemService.editAndUpdate( fixture.getSelectedRow() ) ).andReturn( custItem12 );
EasyMock.replay(distributorItemService);


// add additional test code here
EasyMock.verify(distributorItemService);
}
Even this also gets failed, saying the expection is actually "0".

Here arises a series of questions to me:
1) Even if i expect in the SetDistributorItemService_1() method a mock that i created in this particular method how will it be used in my EditAction method.
There is no way a relation between testSetDistributorItemService_1() method & the testEditAction_1() method.
2) I have another question is that y are the (interfaces) mock services objects are created in each and every method? Why cant they be created in one method which can be the setup() method? Does the tool do this? Am i missing any settings?
3) Even if i expect where do i need to expect? Is it in the testSetDistributorItemService_1() method or the testEditAction_1() method?
4) Why not the verify & reply methods are not called in each and ever method as what i see that is generated in the testSetDistributorItemService_1() method.
Coz without them i cant even expect anything?
This might be the bigger query but hope & expect a reply to this, i have even sent this same content to your mail.
pavi4u143
 
Posts: 7
Joined: Mon Apr 20, 2009 3:38 am


Return to CodePro AnalytiX & PlusPak. EclipsePro Audit & Test

Who is online

Users browsing this forum: No registered users and 1 guest