You need to sign in to do that
Don't have an account?
Test Coverage Help
I wrote the test class and i am getting 70% code coverage however my controller class has the wrapper class ,here i was struck. can any one help me to write the testcode for wrapperclass.
Here Is My controller Class
========================
/****************************************************************************
* Custom controller for the managing a list of expenses for an expense sheet
* Manages a list of expense records, holding insert/delete
* information in memory until the user chooses to save the changes.
***************************************************************************/
public with sharing class ExpenseListEditController
{
// wrapper classes for the contacts being managed
public List<ExpenseKeyWrapper> wrappers {get; set;}
// when a user chooses to add items, the number of
// items to add will be present in this property
public Integer addItemCount {get; set;}
// when a user deletes a record, the record key will
// be present in this property
public Integer keyToDelete {get; set;}
// the unique record key master value
public Integer mainKey {get; set;}
public Expense_Sheet__c expSheet {get; set;}
// string to hold Expense sheet Is
public string idParam;
// the records to delete when the user saves their work
private List<Expense__c> toDelete=new List<Expense__c>();
// constructor
public ExpenseListEditController()
{
mainKey=1;
addItemCount=3;
idParam = apexpages.currentpage().getparameters().get('Id');
buildexpensesheet();
}
Private void buildexpensesheet() {
wrappers=new List<ExpenseKeyWrapper>();
expSheet = [Select Name, Employee__c, Month__c, Year__c , total__c FROM Expense_Sheet__c WHERE id =:idParam];
// get the current expenses from the database for the current expense sheets
List<Expense__c> expenses=[select id, Date__c,Receipt_Number__c, Description__c, Type__c, Amount__c, Vat_Code__c, Expense_Sheet__c from Expense__c WHERE Expense_Sheet__c = :idParam LIMIT 1000];
for (Expense__c exps : expenses)
{
wrappers.add(new ExpenseKeyWrapper(mainKey++, exps));
}
}
// add a number of items to the list
public PageReference addItems()
{
if ( (addItemCount>0) && (addItemCount<10) )
{
for (Integer idx=0; idx<addItemCount; idx++)
{
wrappers.add(new ExpenseKeyWrapper(mainKey++, new Expense__c()));
}
}
return null;
}
// remove (delete) an item from the list
public PageReference removeItem()
{
Integer idx=0;
Boolean found=false;
for (ExpenseKeyWrapper wrap : wrappers)
{
if (wrap.key==keyToDelete)
{
found=true;
if (null!=wrap.expense.id)
{
toDelete.add(wrap.expense);
}
break;
}
else
{
idx++;
}
}
if (found)
{
wrappers.remove(idx);
}
return null;
}
// save the users work
public PageReference save()
{
PageReference result=null;
Boolean error=false;
List<Expense__c> toUpsert=new List<Expense__c>();
// make sure that if any data has been entered, the
// last name is present as this is required
for (ExpenseKeyWrapper wrapper : wrappers)
{
if ( (!String.IsBlank(wrapper.expense.Description__c)) )
{
if (String.IsBlank(wrapper.expense.Expense_Sheet__c))
{
if (String.IsBlank(wrapper.expense.Receipt_Number__c))
wrapper.expense.Expense_Sheet__c = idParam;
}
toUpsert.add(wrapper.expense);
}
else
{
error=true;
}
}
if (!error)
{
delete toDelete;
upsert toUpsert;
// return the user to the contacts tab
result=new PageReference('/' + idParam);
}
return result;
}
public PageReference updatetotal()
{
Boolean error=false;
List<Expense__c> toUpsert=new List<Expense__c>();
// make sure that if any data has been entered, the
// last name is present as this is required
for (ExpenseKeyWrapper wrapper : wrappers)
{
if ( (!String.IsBlank(wrapper.expense.Description__c)) )
{
if (String.IsBlank(wrapper.expense.Expense_Sheet__c))
{
wrapper.expense.Expense_Sheet__c = idParam;
}
toUpsert.add(wrapper.expense);
}
else
{
error=true;
}
}
if (!error)
{
delete toDelete;
upsert toUpsert;
}
buildexpensesheet();
return Null;
}
}
Test Class for this (Getting 70% code coverage) suggest any one writing test class for wrapper class
================================================================================
@isTest(seeAllData = true)
Public class TestExpenseListEditController{
public Integer idx = 0;
public Integer addItemCount = 10;
public Integer keyToDelete = 21;
public Integer mainKey = 12;
public string idParam = 'First String';
Public boolean found = true;
//Public boolean found = true;
Public PageReference result = null;
Public Boolean error = false;
public static testMethod void TestExpenseListEditController(){
Integer inKey = 52;
SFDC_Employee__c se = new SFDC_Employee__c();
se.Name = 'Indian';
Insert se;
Expense_Sheet__c es = new Expense_Sheet__c();
es.Approved__c = true;
es.Month__c = 'January';
es.Year__c = '2013';
es.Employee__c = se.Id;
Insert es;
Expense_Sheet__c es1 = new Expense_Sheet__c();
es1.Approved__c = true;
es1.Month__c = 'January';
es1.Year__c = '2013';
es1.Employee__c = se.Id;
Insert es1;
Expense_Sheet__c es2 = new Expense_Sheet__c();
es2.Approved__c = true;
es2.Month__c = 'January';
es2.Year__c = '2013';
es2.Employee__c = se.Id;
Insert es2;
Apexpages.currentpage().getparameters().put('Id',es.id);
Expense__c ep = new Expense__c();
ep.Description__c = 'First test';
ep.Expense_Sheet__c = es.Id;
ep.Receipt_Number__c = '1234567';
ep.Type__c = 'Equipment';
ep.Vat_Code__c = 'First test';
Insert ep;
Expense__c ep1 = new Expense__c();
ep1.Description__c = 'First test';
ep1.Expense_Sheet__c = es.Id;
ep1.Receipt_Number__c = '1234567';
ep1.Type__c = 'Equipment';
ep1.Vat_Code__c = 'First test';
Insert ep1;
Expense__c ep2 = new Expense__c();
ep2.Description__c = 'First test';
ep2.Expense_Sheet__c = es.Id;
ep2.Receipt_Number__c = '1234567';
ep2.Type__c = 'Equipment';
ep2.Vat_Code__c = 'First test';
Insert ep2;
Upsert ep2;
Delete ep2;
ExpenseKeyWrapper eWrapper = new ExpenseKeyWrapper(inKey,ep);
System.assertEquals(inKey, eWrapper.key);
//System.assertEquals(ep1, eWrapper.ep1);
ExpenseListEditController ELEC = new ExpenseListEditController();
ELEC.addItems();
ELEC.removeItem();
ELEC.Save();
ELEC.updatetotal();
}
}