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
Sunay - KVP Bus SolnsSunay - KVP Bus Solns 

Help on writing test class

Hi,

 

Can anyone please help me on writing test class for the below Apex class:

 

public Voucher()
{

voucherId ='';

statusmessage='';

guestId = ApexPages.currentPage().getParameters().get('gId');

rewardId = ApexPages.currentPage().getParameters().get('rwId');

Guest_Profile__c gp = [Select id ,Guest_Name__c, Points_Balance__c from Guest_Profile__c where id=:guestId ];

Reward__c reward = [Select id ,Points__c,Voucher_Images_Html__c from Reward__c where id=:rewardId ];

guestName = gp.Guest_Name__c;

htmlsource=reward.Voucher_Images_Html__c ;

currentPoints = gp.Points_Balance__c;

rewardpoints = reward.Points__c;

}


public Pagereference voucherNumber()
{


Voucher__c voucher = new Voucher__c();

Guest_Profile__c gp = [Select id , Points_Balance__c from Guest_Profile__c where id=:guestId ];

Reward__c reward = [Select id ,Points__c from Reward__c where id=:rewardId ];

currentPoints = gp.Points_Balance__c ;

if((currentPoints -reward.Points__c) < 0)

statusmessage='Please get More Points to Redeem this Reward.';

else

currentPoints = currentPoints -reward.Points__c;

Redemption_History__c redeem = new Redemption_History__c() ;

redeem.Guest_Name__c = gp.id;

redeem.Redeemed_Points__c = reward.Points__c;

redeem.Remaining_Points__c = currentPoints ;

redeem.Reward_Name__c = reward.id;

insert voucher;

redeem.Voucher_No__c = voucher.id;

insert redeem;

voucherId = voucher.id;

return null;

}

 

Kindly do the needful asap.

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

try like this.

 

 

@isTest
private class TestVoucher{
static testMethod void testVouchermethod() {
ApexPages.currentPage().getParameters().put('gId', respective id value);
ApexPages.currentPage().getParameters().put('rwId', respective id value);

preapare the test data here......like Guest_Profile__c records and Reward__c records.....

Voucher obj = new Voucher();

obj.voucherNumber();

}
}

All Answers

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code as reference for test method class:
@isTest
private class testTriggerinsert_Contact_Activity
{
public static testMethod void unitTestinsert_Contact_Activity4Task()
{
Voucher v=new Voucher();
v. voucherNumber;

}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

kiranmutturukiranmutturu

try like this.

 

 

@isTest
private class TestVoucher{
static testMethod void testVouchermethod() {
ApexPages.currentPage().getParameters().put('gId', respective id value);
ApexPages.currentPage().getParameters().put('rwId', respective id value);

preapare the test data here......like Guest_Profile__c records and Reward__c records.....

Voucher obj = new Voucher();

obj.voucherNumber();

}
}

This was selected as the best answer
Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi Kiran,

 

Need help on the below trigger:

 

I was trying to write the test method for the below trigger and I was able to create required records and covered 19%. But further I am not able to go ahead with not even 1 %. 

 

trigger membership_for_loyalty_programme on Transaction_Breakup__c (before insert,before update)
{

Boolean calculate_points=false;

for(Transaction_Breakup__c tbp:trigger.new)
{
List< Loyalty_Program__c> Loyalty_Program_List;

Membership__c Membership_Selected;

Visit_Details__c vis =[Select Property_Wise_Profile__c from Visit_Details__c where id=:tbp.Transaction_Detail_No__c];

Property_Wise_Profile__c pwr =[Select id,No_of_Room_Nights__c, Loyalty_Programme_Record_Type__c, No_of_Stays__c,Property__c,Guest_Profiile_Id__c,Property_Name__c, Total_expenditure_against_property__c from Property_Wise_Profile__c where id=:vis.Property_Wise_Profile__c ];

Loyalty_Program_List = [select id, Name, Automatically_Assigned__c, Consider_Tax_for_Overall_Expense__c, Upgrade_Memberships_Automatically__c, RecordType.Name, No_of_Stays__c,No_of_Room_Nights__c, Overall_Expense__c, Duration__c from Loyalty_Program__c where Active__c = true ORDER BY Overall_Expense__c, No_of_Room_Nights__c, No_of_Stays__c ];

try
{
if(Loyalty_Program_List.size() > 0 )
{

Loyalty_Program__c Loyalty_Program_Selected=new Loyalty_Program__c(No_of_Room_Nights__c =0,Name='temp',No_of_Stays__c =0,Overall_Expense__c=0) ;

System.debug(Loyalty_Program_Selected);

for(Loyalty_Program__c Loyalty_Program:Loyalty_Program_List)
{

Date minDate =Date.Today()-(Integer.valueof(Loyalty_Program.Duration__c)*30);

AggregateResult groupedResults;

if(Loyalty_Program.Consider_Tax_for_Overall_Expense__c==true)

groupedResults = [SELECT SUM(Total_Amount_Incl_Tax__c) SumExp,SUM(No_of_Room_Nights__c) SumNights from Visit_Details__c where Property_Wise_Profile__c=:pwr.Id AND Check_In_Date1__c >=:minDate ];

else

groupedResults = [SELECT SUM(Total_Amount_Excl_Tax__c) SumExp,SUM(Room_Nights__c) SumNights from Visit_Details__c where Property_Wise_Profile__c=:pwr.Id AND Check_In_Date1__c >=:minDate ];

Integer number_of_stays = [SELECT COUNT() from Visit_Details__c where Property_Wise_Profile__c=:pwr.Id AND Check_In_Date1__c >=:minDate ];

// if(Loyalty_Program.RecordType.Name=='Stay Based Membership')
{

if((number_of_stays >= Loyalty_Program.No_of_Stays__c)&&(Loyalty_Program_Selected.No_of_Stays__c<Loyalty_Program.No_of_Stays__c))
{

Loyalty_Program_Selected = Loyalty_Program ;

}

}

// if(Loyalty_Program.RecordType.Name=='Room Night based Membership')
{
if(((Decimal)groupedResults.get('SumNights') >= Loyalty_Program.No_of_Room_Nights__c)&&(Loyalty_Program_Selected.No_of_Room_Nights__c<Loyalty_Program.No_of_Room_Nights__c) )
{
Loyalty_Program_Selected = Loyalty_Program ;
}

}
// if(Loyalty_Program.RecordType.Name=='Revenue Based Membership')
{

Decimal Overall_Amount=0.0;

if((Loyalty_Program_Selected.Overall_Expense__c<Loyalty_Program.Overall_Expense__c))
{

if(Loyalty_Program.Consider_Tax_for_Overall_Expense__c==true )

Overall_Amount = (Decimal)groupedResults.get('SumExp')+tbp.Amount_Spent__c+tbp.Tax_Amount__c;

else

Overall_Amount = (Decimal)groupedResults.get('SumExp')+tbp.Amount_Spent__c;

if(Overall_Amount>=Loyalty_Program.Overall_Expense__c )
{

Loyalty_Program_Selected = Loyalty_Program ;

}

}

}
}

List< Membership__c> Membership_List =[select id,Active__c,Test__c,Loyalty_Program_Name__c,Loyalty_Program_Name__r.Id from Membership__c where Guest_Name__c =: pwr.Guest_Profiile_Id__c AND Property_Name__c =: pwr.Property__c];

if(Membership_List.size() > 0)
{
calculate_points=true;

Boolean selected =false;

for(Membership__c Membership:Membership_List)
{
if((Membership.Loyalty_Program_Name__r.Id!=Loyalty_Program_Selected.Id)&&(Loyalty_Program_Selected.Name!='temp'))
{
Membership.Active__c = false;


System.debug('BBBB');
}
else
{
Membership.Active__c = true;


System.debug('AAAAAA');

selected = true;


}

update Membership;

Membership_Selected = Membership;
}

if((!selected)&&(Loyalty_Program_Selected.Name!='temp')&&(Loyalty_Program_Selected.Automatically_Assigned__c == true))
{

Membership__c Membership_New =new Membership__c();

Membership_New.Guest_Name__c = pwr.Guest_Profiile_Id__c;

Membership_New.Property_Name__c=pwr.Property__c;

Membership_New.Loyalty_Program_Name__c =Loyalty_Program_Selected.id;

Membership_New.Active__c = true;

insert Membership_New;

Membership_Selected = Membership_New;

}

if((Loyalty_Program_Selected.Name!='temp')&& (Loyalty_Program_Selected.Automatically_Assigned__c == false))
{

Guest_Profile__c gp =[select id,Eligible_Loyalty_Program__c,Loyalty_programme_assigned_property__c from Guest_Profile__c where id =:pwr.Guest_Profiile_Id__c];

gp.Eligible_Loyalty_Program__c =Loyalty_Program_Selected.Name;

gp.Loyalty_programme_assigned_property__c =pwr.Property_Name__c;

update gp;
}

}
else
{

if((Loyalty_Program_Selected.Name!='temp')&& (Loyalty_Program_Selected.Automatically_Assigned__c == true))
{

Membership__c Membership_New =new Membership__c();

Membership_New.Guest_Name__c = pwr.Guest_Profiile_Id__c;

Membership_New.Property_Name__c=pwr.Property__c;

Membership_New.Test__c ='test';

Membership_New.Loyalty_Program_Name__c =Loyalty_Program_Selected.id;

Membership_New.Active__c = true;

insert Membership_New;

calculate_points=true;

Membership_Selected = Membership_New;


}
if((Loyalty_Program_Selected.Name!='temp')&& (Loyalty_Program_Selected.Automatically_Assigned__c == false))
{

calculate_points=false;

Guest_Profile__c gp =[select id,Eligible_Loyalty_Program__c,Loyalty_programme_assigned_property__c from Guest_Profile__c where id =:pwr.Guest_Profiile_Id__c];

gp.Eligible_Loyalty_Program__c =Loyalty_Program_Selected.Name;

gp.Loyalty_programme_assigned_property__c =pwr.Property_Name__c;

update gp;

}
}



Membership__c Membership_Active =[select id,Active__c,Test__c,Loyalty_Program_Name__c,Loyalty_Program_Name__r.Id from Membership__c where Guest_Name__c =: pwr.Guest_Profiile_Id__c AND Property_Name__c =: pwr.Property__c AND Active__c =: true];




if(calculate_points)
{
Point_Conversion__c pc;
try
{

System.debug(Membership_Selected.Loyalty_Program_Name__c);
pc =[Select id, Active__c,Service_Type__c,Split_Beverage__c,Split_by_Service_Type__c,Split_Room_Charges__c,Amount_in_Consideration__c,Point_Calculation_By__c, Calculate_Discounts__c, Calculate_Points__c, Calculate_Points_for_Tax_Amount__c,
Consider_Discount__c, Discount_Consideration__c, Property_Name__c, Revenue_Code__c, Revenue_Type__c, Revenue_Type_Formula__c, X1_point__c, Amount__c, Points__c, Discount_for_Total_Amount__c, Dicount_Percent__c, Applicable_From__c, Beverage_Amount_for_Points__c, Beverage_Discount__c, Beverage_Points__c, Food_Amount_for_Points__c, Food_Discount__c, Food_Points__c, Liquor_Amount_for_Points__c, Liquor_Discount__c, Liquor_Points__c, Other_Amount_for_Points__c, Other_Discount__c, Other_Points__c, Plan_Amount_for_Points__c, Plan_Discount__c, Plan_Points__c, Room_Amount_for_Points__c, Room_Discount__c, Room_Points__c, Soft_Drink_Amount_for_Points__c, Soft_Drink_Discount__c, Soft_Drink_Points__c, Tax_Amount_for_Points__c, Tax_Discount__c, Tax_Points__c, Tobacco_Amount_for_Points__c, Tobacco_Discount__c, Tobacco_Points__c from Point_Conversion__c where Loyalty_Program_Name__c =:Membership_Active .Loyalty_Program_Name__c AND Active__c=:true];





if(pc.Split_by_Service_Type__c ==true)
{


if(pc.Amount_in_Consideration__c =='Bill Amount inc. Tax')
{


if(pc.Calculate_Points_for_Tax_Amount__c==true)
{

if((tbp.Service_Type__c.contains('Food'))&&(pc.Calculate_Points__c==true)&&(pc.Food_Amount_for_Points__c!=0) )
{
tbp.Points_for_Food__c =tbp.Points_for_Food__c+((tbp.Amount_for_Food__c+tbp.Tax_for_Food__c)*(pc.Food_Points__c/pc.Food_Amount_for_Points__c)) ;

}


// ------------------------------Beverage-----------------------------

if( (tbp.Service_Type__c.contains('Beverage'))&&(pc.Calculate_Points__c==true)&&(pc.Beverage_Amount_for_Points__c!=0) )
{
tbp.Points_for_Beverage__c =tbp.Points_for_Beverage__c+((tbp.Amount_for_Beverage__c+tbp.Tax_for_Beverage__c)*(pc.Beverage_Points__c/pc.Beverage_Amount_for_Points__c)) ;

}


// ------------------------------Liquor-----------------------------

if( (tbp.Service_Type__c.contains('Liquor'))&&(pc.Calculate_Points__c==true)&&(pc.Liquor_Amount_for_Points__c!=0) )
{
tbp.Points_for_Liquor__c =tbp.Points_for_Liquor__c+((tbp.Amount_for_Liquor__c+tbp.Tax_for_Liquor__c)*(pc.Liquor_Points__c/pc.Liquor_Amount_for_Points__c)) ;

}


// ------------------------------Soft_Drink-----------------------------

if( (tbp.Service_Type__c.contains('Soft Drinks'))&&(pc.Calculate_Points__c==true)&&(pc.Soft_Drink_Amount_for_Points__c!=0) )
{
tbp.Points_for_Soft_Drink__c =tbp.Points_for_Soft_Drink__c+((tbp.Amount_for_Soft_Drink__c+tbp.Tax_for_Soft_Drink__c)*(pc.Soft_Drink_Points__c/pc.Soft_Drink_Amount_for_Points__c)) ;

}


// ------------------------------Tobacco-----------------------------

if( (tbp.Service_Type__c.contains('Tobacco'))&&(pc.Calculate_Points__c==true)&&(pc.Tobacco_Amount_for_Points__c!=0) )
{
tbp.Points_for_Tobacco__c =tbp.Points_for_Tobacco__c+((tbp.Amount_for_Tobacco__c+tbp.Tax_for_Tobacco__c)*(pc.Tobacco_Points__c/pc.Tobacco_Amount_for_Points__c)) ;

}

// ------------------------------Others-----------------------------
if( (tbp.Service_Type__c.contains('Others'))&&(pc.Calculate_Points__c==true)&&(pc.Other_Amount_for_Points__c!=0) )
{
tbp.Points_for_Others__c =tbp.Points_for_Others__c+((tbp.Amount_for_Others__c+tbp.Tax_for_Others__c)*(pc.Other_Points__c/pc.Other_Amount_for_Points__c)) ;
}

// ------------------------------Room-----------------------------
if( (tbp.Service_Type__c.contains('Room'))&&(pc.Calculate_Points__c==true)&&(pc.Room_Amount_for_Points__c!=0) )
{
tbp.Points_for_Room__c =tbp.Points_for_Room__c+((tbp.Amount_for_Room__c )*(pc.Room_Points__c/pc.Room_Amount_for_Points__c)) ;
}

// ------------------------------Plan-----------------------------
if( (tbp.Service_Type__c.contains('Plan'))&&(pc.Calculate_Points__c==true)&&(pc.Plan_Amount_for_Points__c!=0) )
{
tbp.Points_for_Plan__c =tbp.Points_for_Others__c+((tbp.Amount_for_Plan__c )*(pc.Plan_Points__c/pc.Plan_Amount_for_Points__c)) ;
}


}

if(pc.Calculate_Points_for_Tax_Amount__c==false)
{


if((pc.Service_Type__c.contains('Food'))&&(pc.Calculate_Points__c==true)&&(pc.Food_Amount_for_Points__c!=0) )
{
tbp.Points_for_Food__c =tbp.Points_for_Food__c+((tbp.Amount_for_Food__c )*(pc.Food_Points__c/pc.Food_Amount_for_Points__c)) ;

}

// ------------------------------Beverage-----------------------------

if( (tbp.Service_Type__c.contains('Beverage'))&&(pc.Calculate_Points__c==true)&&(pc.Beverage_Amount_for_Points__c!=0) )
{
tbp.Points_for_Beverage__c =tbp.Points_for_Beverage__c+((tbp.Amount_for_Beverage__c )*(pc.Beverage_Points__c/pc.Beverage_Amount_for_Points__c)) ;

}

// ------------------------------Liquor-----------------------------

if( (tbp.Service_Type__c.contains('Liquor'))&&(pc.Calculate_Points__c==true)&&(pc.Liquor_Amount_for_Points__c!=0) )
{
tbp.Points_for_Liquor__c =tbp.Points_for_Liquor__c+((tbp.Amount_for_Liquor__c )*(pc.Liquor_Points__c/pc.Liquor_Amount_for_Points__c)) ;

}


// ------------------------------Soft_Drink-----------------------------

if( (tbp.Service_Type__c.contains('Soft Drinks'))&&(pc.Calculate_Points__c==true)&&(pc.Soft_Drink_Amount_for_Points__c!=0) )
{
tbp.Points_for_Soft_Drink__c =tbp.Points_for_Soft_Drink__c+((tbp.Amount_for_Soft_Drink__c )*(pc.Soft_Drink_Points__c/pc.Soft_Drink_Amount_for_Points__c)) ;

}


// ------------------------------Tobacco-----------------------------

if( (tbp.Service_Type__c.contains('Tobacco'))&&(pc.Calculate_Points__c==true)&&(pc.Tobacco_Amount_for_Points__c!=0) )
{
tbp.Points_for_Tobacco__c =tbp.Points_for_Tobacco__c+((tbp.Amount_for_Tobacco__c )*(pc.Tobacco_Points__c/pc.Tobacco_Amount_for_Points__c)) ;

}

// ------------------------------Room-----------------------------
if( (tbp.Service_Type__c.contains('Room'))&&(pc.Calculate_Points__c==true)&&(pc.Room_Amount_for_Points__c!=0) )
{
tbp.Points_for_Room__c =tbp.Points_for_Room__c+((tbp.Amount_for_Room__c )*(pc.Room_Points__c/pc.Room_Amount_for_Points__c)) ;
}

// ------------------------------Plan-----------------------------
if( (tbp.Service_Type__c.contains('Plan'))&&(pc.Calculate_Points__c==true)&&(pc.Plan_Amount_for_Points__c!=0) )
{
tbp.Points_for_Plan__c =tbp.Points_for_Others__c+((tbp.Amount_for_Plan__c )*(pc.Plan_Points__c/pc.Plan_Amount_for_Points__c)) ;
}


// ------------------------------Tax-----------------------------
if( (pc.Calculate_Points__c==true)&&(pc.Tax_Amount_for_Points__c!=0) )
{
tbp.Points_for_Tax__c =tbp.Points_for_Tax__c+((tbp.Amount_for_Tax__c )*(pc.Tax_Points__c/pc.Tax_Amount_for_Points__c)) ;
}

}


}

}
else
{
if((pc.Amount_in_Consideration__c=='Bill Amount inc. Tax')&&(pc.Calculate_Points__c==true)&&(pc.Amount__c!=0) )
{

tbp.Points_Earned__c =tbp.Points_Earned__c+((tbp.Amount_Spent__c+tbp.Tax_Amount__c)*(pc.Points__c/pc.Amount__c)) ;

}

if((pc.Amount_in_Consideration__c=='Bill Amount exl. Tax')&&(pc.Calculate_Points__c==true)&&(pc.Amount__c!=0) )
{
tbp.Points_Earned__c =tbp.Points_Earned__c+((tbp.Amount_Spent__c)*(pc.Points__c/pc.Amount__c)) ;

}
}

}
catch(Exception e)
{
//tbp.addError(String.valueOf( e.getMessage() ));
}

}

}
}
catch(Exception e)
{
//tbp.addError(e.getMessage()+'123');

}
}
}