Code Pro Test Case generation problem

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 Test Case generation problem

Postby pavi4u143 » Mon Apr 20, 2009 4:00 am

Hi,
I am trying to generate the test case for my one of the class, but the problem is the codePro is not generating the mock object, nor expecting, nor verifying & nor replaying in every method for the service class but its generating the mock object in the test case method for the same service class method(See below)

public void testSetDistributorItemService_fixture_1()
throws Exception
{
DistributorItemBean fixture2 = getFixture();
DistributorItemService distributorItemService = EasyMock.createMock(DistributorItemService.class);
// add mock object expectations here

EasyMock.replay(distributorItemService);

fixture2.setDistributorItemService(distributorItemService);

// add additional test code here
EasyMock.verify(distributorItemService);
}

DistributorItemService is my service class for which i had the setter method created in the Bean class. I tried to generate the test case for my bean class which generated this method also for the service setter method.
So is it possible to create the mock object at every method level & why is not the codePro expecting for the methods.

public void testEditAction_fixture_1()
throws Exception
{
DistributorItemBean fixture2 = getFixture();

fixture2.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:346)
}

This is one of the test case method generated for one of my class shown below. The NullPointerException is caused since the distributorItemService is coming null.
public void editAction ( )
{
this.setCustDistItem( this.distributorItemService
.editAndUpdate( this.selectedRow ) );
this.setNewDistributorId( this.custDistItem
.getCustDistributor().getId() );

// Disable the DistributorCode text-box since its edit option
this.setCheckCode( true );

this.setSaveUpdateButton( RMConstants.BUTTON_NAME_UPDATE );
if ( this.isDisableUpdate() )
{
this.setAddEditLabel( RMConstants.HEADER_NAME_VIEW );
}
else
{
this.setAddEditLabel( RMConstants.HEADER_NAME_EDIT );
}
this.setAddEditFlag( false );
}

Here you can see that i have a call to my service class method.


Its something that i need to always work or edit, make a change to the generated test case every time which is too annoying. Can anyone suggest me the way or solution or even suggestion. Early replies are deeply appreciated.
pavi4u143
 
Posts: 7
Joined: Mon Apr 20, 2009 3:38 am

Re: Code Pro Test Case generation problem

Postby dcarew » Mon Apr 20, 2009 12:15 pm

Hi Pavi,

Sorry to hear you're having problems. CodePro uses mocks in two different ways, which is one reason you're seeing differences in the generated tests. If the variable to be mocked is a parameter to a method under test, then we create the mock and verify that the expected methods were called. This is because we assume that the behavior of what happened to the mock in the method under test is important to the success or failure of the test. The other way we use mocks is for injecting values into fixtures. These mocks will be used in the method under test, but aren't necessarily part of the test itself. These two modes are a judgment call we made - that one use of a mock is part of the test and the other use isn't. Whether this is true or not depends on the particular situation.

We do generate tests for setter and getter methods, even if nothing particularly interesting happens in the setter/getter. You can ignore these tests, or delete them after the generation.

When I run some sample code similar to your code, I see slightly different behavior. CodePro generates two fixtures. One creates an action class with a null service, and the other creates an action class with a mocked service. This is because both the null and mocked service are valid possibilities for the fixture's field from CodePro's point of view. The test using the null fixture field does throw a NPE. You can delete this test if it is not interesting to you. Is the test with the mocked service not created for you?

If you're not seeing it, there are some preferences you can play with to try and get it to work. First, in CodePro > JUnit > Generated Code > Fixtures, uncheck the 'Generate test fixture as field' checkbox. If that doesn't create better tests, try using the data flow test generator. In CodePro > JUnit > Generated Code, check the 'Enable flow path based test generator'. This will use different logic when generating the tests.

Hope this helps,

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

Re: Code Pro Test Case generation problem

Postby pavi4u143 » Thu Apr 23, 2009 10:11 pm

Hi dcarew,
Thanks for your reply.
i played around as like what you have mentioned the two ways but i found that both the ways are not passing the methods and so obviously the coverage on this method(s) coming as zero '0'. Still i am getting the NullPtrException.
The method is shown below:
/**
338 * The Edit Action method is called when the user clicks on the edit button
339 * from the maintainUDI page. The method populates with all the details in
340 * the AddEditUDI page.
341 */
342 @Secured("ACTION_RM_RECIPE_MAINTAINER")
343 public void editAction ( )
344 {
345 0 this.setCustDistItem( this.distributorItemService
346 0 .editAndUpdate( this.selectedRow ) );
347 0 this.setNewDistributorId( this.custDistItem
348 0 .getCustDistributor().getId() );
349
350 // Disable the DistributorCode text-box since its edit option
351 0 this.setCheckCode( true );
352
353 0 this.setSaveUpdateButton( RMConstants.BUTTON_NAME_UPDATE );
354 0 if ( this.isDisableUpdate() )
355 {
356 0 this.setAddEditLabel( RMConstants.HEADER_NAME_VIEW );
357 }
358 else
359 {
360 0 this.setAddEditLabel( RMConstants.HEADER_NAME_EDIT );
361 }
362 0 this.setAddEditFlag( false );
363 0 }

and the test case generated for this method are as below :
/**
* Run the void editAction() method test.
*
* @generatedBy CodePro at 4/24/09 11:02 AM
*/
public void testEditAction_1()
throws Exception
{
DistributorItemBean fixture = new DistributorItemBean();
CustItem custItem = new CustItem();
CustDistributor custDistributor = new CustDistributor();
custDistributor.setId(new Long(0L));
custItem.setCustDistributor(custDistributor);
fixture.setCustDistItem(custItem);
fixture.setDisableUpdate(false);
fixture.setCheckCode(true);
fixture.setDistCode(new Long(0L));
fixture.setItemNum("");
fixture.setStoreAllUOM(new HashMap<String, String>());
fixture.setUserProfileDTO(new UserProfileDTO());
fixture.setDeleteRow(new Long(0L));
fixture.setStoreWeightUOM(new HashMap<String, String>());
fixture.setAddEditDistributorList(new ArrayList<SelectItem>());
fixture.setSelectedRow(new Long(0L));
fixture.setDistributorItemService(EasyMock.createNiceMock(DistributorItemService.class));
fixture.setItemDesc("");
fixture.setAddEditLabel("");
fixture.setRmUOMService(EasyMock.createNiceMock(RMUOMService.class));
fixture.setDistributorsList(new ArrayList<SelectItem>());
fixture.setAddEditFlag(true);
fixture.setNewDistributorId(new Long(0L));
fixture.setDistributorService(EasyMock.createNiceMock(DistributorService.class));
fixture.setSaveUpdateButton("");
fixture.setItemList(new ArrayList<CustItem>());
fixture.setDeleteItemFlag(true);
fixture.setCustDistributorList(new ArrayList<CustDistributor>());

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)
}

/**
* Run the void editAction() method test.
*
* @generatedBy CodePro at 4/24/09 11:02 AM
*/
public void testEditAction_2()
throws Exception
{
DistributorItemBean fixture = new DistributorItemBean();
CustItem custItem = new CustItem();
CustDistributor custDistributor = new CustDistributor();
custDistributor.setId(new Long(0L));
custItem.setCustDistributor(custDistributor);
fixture.setCustDistItem(custItem);
fixture.setDisableUpdate(true);
fixture.setCheckCode(true);
fixture.setDistCode(new Long(0L));
fixture.setItemNum("");
fixture.setStoreAllUOM(new HashMap<String, String>());
fixture.setUserProfileDTO(new UserProfileDTO());
fixture.setDeleteRow(new Long(0L));
fixture.setStoreWeightUOM(new HashMap<String, String>());
fixture.setAddEditDistributorList(new ArrayList<SelectItem>());
fixture.setSelectedRow(new Long(0L));
fixture.setDistributorItemService(EasyMock.createNiceMock(DistributorItemService.class));
fixture.setItemDesc("");
fixture.setAddEditLabel("");
fixture.setRmUOMService(EasyMock.createNiceMock(RMUOMService.class));
fixture.setDistributorsList(new ArrayList<SelectItem>());
fixture.setAddEditFlag(true);
fixture.setNewDistributorId(new Long(0L));
fixture.setDistributorService(EasyMock.createNiceMock(DistributorService.class));
fixture.setSaveUpdateButton("");
fixture.setItemList(new ArrayList<CustItem>());
fixture.setDeleteItemFlag(true);
fixture.setCustDistributorList(new ArrayList<CustDistributor>());

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)
}

Both still throw the NullPointerExeption. I had the following setting as u had said CodePro > JUnit > Generated Code, check the 'Enable flow path based test generator', and in the Fixtures i have unselected the 'Generate test fixture as field'.
Its something like as below i am expecting the tool to generate :
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 );
}
where in the mock object for the distributorItemService is created in the Setup method, and the expect ,reply & verify are all inthe same method.

Is it that the test cases are not generated fully means is it necessary to edit the test cases every time generated?
Also to say actually non of the Bean methods that have the Service Class method calls are getting covered with this. I donno waht i am missing or whether any setting is lost or what?
I am totally confused or you can say like almost lost in what to do, how to generate the test case with this tool has been the big question to me.
Please help on this ASAP! :oops: :roll: :idea: :?:
pavi4u143
 
Posts: 7
Joined: Mon Apr 20, 2009 3:38 am

Re: Code Pro Test Case generation problem

Postby Brian Wilkerson » Tue Apr 28, 2009 12:06 pm

Brian Wilkerson
Google, Inc.
http://www.google.com
Brian Wilkerson
Moderator
 
Posts: 32
Joined: Fri Apr 28, 2006 9:37 am


Return to CodePro AnalytiX & PlusPak. EclipsePro Audit & Test

Who is online

Users browsing this forum: No registered users and 1 guest