function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marcio Zima.ax1538Marcio Zima.ax1538 

Test class for Dynamic Related List code

Hi,

 

I'm trying to code an Apex test class for a Dynamic Related List that add and delete rows in a table through visualforce.

The complete code I got here:

 http://visualforcemadeeasy.blogspot.com.br/2009/03/how-to-add-and-delete-row-in-datatable.html

 

public class AccountsController {

/* I set the Account and Case Objects
here for use through out the code*/
public Account acct { get; private set;}
public Case[] caseItems { get; private set; }
private ApexPages.StandardController controller;

// constructor, loads the Account and
// any cases associated with it

void caseItems(id id) {
acct = [SELECT Id, Name, Type, AccountNumber, Site,
      (SELECT Id, CaseNumber, Status, Reason,Origin,
      Subject FROM Cases) FROM Account
      where id = :id limit 1];
//Hook caseItems to the query above
 caseItems = acct.Cases;
}

//Define the id
id accountid;

/* A List Method to delete the Cases assigned*/
public list todelete = new list();

public AccountsController (ApexPages.StandardController c)
{
/* this will kickoff you main page */
controller = c;
/* to get this current Account Id*/
accountid = c.getRecord().id;
/*kick off the init() function*/
init();
}
public AccountsController () {
accountid =
ApexPages.CurrentPage().getParameters().get('id');

init();

}

void init() {
/* load up Cases
basically we defined caseitems up on top, so
when the page loads then caseItems(accountId)
will go through the query and list out the
Items assoicated with it */
caseItems(accountid);  
}

public PageReference save() {
try {
upsert caseItems;
if ( todelete.size() > 0 ) {           
delete todelete;   
}
caseItems(acct.id);
}
catch ( DmlException exc) {
      ApexPages.addMessages(exc);
      return null;
}
return null;
}


/* your Delete functionality*/
public PageReference del() {

string delnumber =
ApexPages.CurrentPage().getParameters().get('delnumber');

system.assert( delnumber != null );
integer gone = -1;
integer i = 0;
  
for ( i=0; i< casenumber ="="" gone =" i;">= 0) {
todelete.add(caseItems.remove(gone) );
}
return null;
}
public PageReference add() {
// insert a new line, after user clicks Add
Case cs =  new Case(
AccountId = acct.id,
Subject = 'hello', Status = 'Low',
Reason = 'Other',Origin='Low'
);
caseItems.add ( cs );
return null;
 }
}

 

 

 

 

My test class coverage 71%. I'm using custom objects. When I include the function del() - DELETE, it returns the following exception: System.AssertException: Assertion Failed

 

        LineItem__c item = new LineItem__c(ObjectId__c = ag.Id);
        insert item;

        ApexPages.StandardController QContracts_Line = new ApexPages.StandardController(item);
	AccountsController qe = new AccountsController(QContracts_Line);
	ApexPages.currentPage().getParameters().put('id', item.Id);
	qe.add();
	qe.save();
        qe.del();

 

Does anyone knows any way to resolve this?

 

Best Answer chosen by Admin (Salesforce Developers) 
Subramani_SFDCSubramani_SFDC
Hi,

Remove this line from ur code..it will work. Assert used in test methods.
public PageReference del() {

system.assert( delnumber != null );

All Answers

Subramani_SFDCSubramani_SFDC
Hi,

Remove this line from ur code..it will work. Assert used in test methods.
public PageReference del() {

system.assert( delnumber != null );
This was selected as the best answer
Marcio Zima.ax1538Marcio Zima.ax1538

Thank you very much!! Until now code coverage in this class reached 95%.

 

Just 2 lines not covered:

 

public PageReference del() {
string delname = ApexPages.CurrentPage().getParameters().get('delname');
//system.assert( delname != null );
integer gone = -1;
integer i = 0;

for ( i=0; i< caseItems.size(); i++ ) {
if (caseItems[i].Name== delname) {
gone = i;
}
}
if ( gone >= 0) {
todelete.add(caseItems.remove(gone) );
}
return null;
}

Subramani_SFDCSubramani_SFDC
Welcome Dude...............


Thanks and regards,
Subra.J
Trinay Technology Solutions