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
GurunathGurunath 

Test Class Help

Please help me anyone writing the test class for this .......

 

while at the time of  writing test class i am facing the problem like List has no rows for assignment to SObject

 

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

public 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;
}
}

asish1989asish1989
@isTest
private class TestclassExpenseListEditController {

    static testMethod void runPositiveTestCases() {
	
		Expense_Sheet__c testexpence = new Expense_Sheet__c(Name ='test');
		insert testexpence;
		Apexpages.currentpage().getparameters().put('Id',testexpence.id);
		 Expense__c testExpense = new  Expense__c( Name ='test');
		 insert testExpense;
		 Expense__c testExpense1 = new  Expense__c(Name ='test',Description__c = 'test',Expense_Sheet__c = testexpence.id);
		 insert testExpense1;
		ExpenseListEditController clsObj = new ExpenseListEditController();
		clsObj.buildexpensesheet();
		clsObj.addItems();
		clsObj.removeItem();
		clsObj.save();
		clsObj.updatetotal();
		
	}
}

 to cover wrapper class take help from google

http://boards.developerforce.com/t5/forums/replypage/board-id/apex/message-id/131466

http://techman97.wordpress.com/tag/wrapper-class/

Abhi_TripathiAbhi_Tripathi