You need to sign in to do that
Don't have an account?
How to write test class for the following class(inner class)
public class MultiAdd1
{
//will hold the account records to be saved
public List<Account>lstAcct = new List<Account>();
//list of the inner class
public List<innerClass> lstInner
{get;set;}
//will indicate the row to be deleted
public String selectedRowIndex
{get;set;}
//no. of rows added/records in the inner class list
public Integer count = 1;
//{get;set;}
////save the records by adding the elements in the inner class list to lstAcct,return to the same page
public PageReference Save()
{
PageReference pr = new PageReference('/apex/MultiAdd');
for(Integer j = 0;j<lstInner.size();j++)
{
lstAcct.add(lstInner[j].acct);
}
insert lstAcct;
pr.setRedirect(True);
return pr;
}
//add one more row
public void Add()
{
count = count+1;
addMore();
}
/*Begin addMore*/
public void addMore()
{
//call to the iner class constructor
innerClass objInnerClass = new innerClass(count);
//add the record to the inner class list
lstInner.add(objInnerClass);
system.debug('lstInner---->'+lstInner);
}/* end addMore*/
/* begin delete */
public void Del()
{
system.debug('selected row index---->'+selectedRowIndex);
lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
count = count - 1;
}/*End del*/
/*Constructor*/
public MultiAdd1(ApexPages.StandardController ctlr)
{
lstInner = new List<innerClass>();
addMore();
selectedRowIndex = '0';
}/*End Constructor*/
/*Inner Class*/
public class innerClass
{
/*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
public String recCount
{get;set;}
public Account acct
{get;set;}
/*Inner Class Constructor*/
public innerClass(Integer intCount)
{
recCount = String.valueOf(intCount);
/*create a new account*/
acct = new Account();
}/*End Inner class Constructor*/
}/*End inner Class*/
}/*End Class*/
{
//will hold the account records to be saved
public List<Account>lstAcct = new List<Account>();
//list of the inner class
public List<innerClass> lstInner
{get;set;}
//will indicate the row to be deleted
public String selectedRowIndex
{get;set;}
//no. of rows added/records in the inner class list
public Integer count = 1;
//{get;set;}
////save the records by adding the elements in the inner class list to lstAcct,return to the same page
public PageReference Save()
{
PageReference pr = new PageReference('/apex/MultiAdd');
for(Integer j = 0;j<lstInner.size();j++)
{
lstAcct.add(lstInner[j].acct);
}
insert lstAcct;
pr.setRedirect(True);
return pr;
}
//add one more row
public void Add()
{
count = count+1;
addMore();
}
/*Begin addMore*/
public void addMore()
{
//call to the iner class constructor
innerClass objInnerClass = new innerClass(count);
//add the record to the inner class list
lstInner.add(objInnerClass);
system.debug('lstInner---->'+lstInner);
}/* end addMore*/
/* begin delete */
public void Del()
{
system.debug('selected row index---->'+selectedRowIndex);
lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
count = count - 1;
}/*End del*/
/*Constructor*/
public MultiAdd1(ApexPages.StandardController ctlr)
{
lstInner = new List<innerClass>();
addMore();
selectedRowIndex = '0';
}/*End Constructor*/
/*Inner Class*/
public class innerClass
{
/*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
public String recCount
{get;set;}
public Account acct
{get;set;}
/*Inner Class Constructor*/
public innerClass(Integer intCount)
{
recCount = String.valueOf(intCount);
/*create a new account*/
acct = new Account();
}/*End Inner class Constructor*/
}/*End inner Class*/
}/*End Class*/
Try below code but small rewuired field error is comming can you tell me in SAVE method
for(Integer j = 0;j<lstInner.size();j++)
{
lstAcct.add(lstInner[j].acct);
}
how the values are comming in this for loop once the values populate in this list lstInner[j].acct it will cover 100%
@IsTest
Private class TestMultiAdd1
{
Static testmethod void MultiAddTest()
{
List<Account> accounts = new List<Account>{};
Account testAccount = new Account(Name='Test Company Name123');
//accounts.add(testAccount);
//insert accounts;
Account testAccount1 = new Account(Name='Test Company Name123');
ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
MultiAdd1 madd = new MultiAdd1(sc);
PageReference pageRef = Page.MultiAdd;
pageRef.getParameters().put('id', String.valueOf(testAccount.Id));
Test.setCurrentPage(pageRef);
MultiAdd1.innerClass aa=new MultiAdd1.innerClass(10);
//madd.lstAcct.add(testAccount1);
//aa.lstInner;
//aa.recCount='4';
//aa.acct=testAccount1;
aa.recCount='test';
aa.acct=testAccount1;
madd.lstInner.add(aa);
madd.lstAcct.add(testAccount);
madd.selectedRowIndex ='5';
madd.Save();
madd.Add();
madd.addMore();
madd.Del();
}
}
Thanks,
Suresh