You need to sign in to do that
Don't have an account?
suji srinivasan
Vf apex class - test class
Hi, how to write test class for vf component Apex class?
can anyone guide me?
Apex class:
public class salarydetail{
public static list<Salary_Detail__c>salary;
//salary = new list<Salary_Detail__c>();
public String salaryId {
get{
if(salaryId == null && ApexPages.currentPage().getParameters().get('id') != null){
salaryId = ApexPages.currentPage().getParameters().get('id');
}
return salaryId;
}
set;
}
public Salary_Detail__c sd {
get{
return [SELECT Id, Employee_ID__c,Date_of_Joining__c,Department__c,Designation__c,
Gender__c,Location__c,Pan_Number__c,Professional_Tax__c,UAN_No__c,BasicAllowance__c,HouseRentAllowance__c,MonthDays__c,Number_of_Days_present__c, Account_No__c,
Skill_up_Allowance__c,PF__c,SpecialAllowance__c,TotalEarnings__c,TotalDeductions__c,
NetPay__c,In_Words__c FROM Salary_Detail__c WHERE Id = :salaryId LIMIT 1];
}
set;
}
}
Test class:
@ IsTest
public class salarydetailtest {
public static Testmethod void Salary(){
Test.startTest();
Employee_Information__c E = new Employee_Information__c();
E.Name='Test';
E.Employee_Last_Name__c ='s';
E.Employee_ID__c = 'E-1234';
E.Date_of_Join__c= system.today();
E.Tax_Regime__c='Old Regime';
E.Confirmed_CTC__c=1500000;
insert E;
Employee_Information__c E1 = new Employee_Information__c();
E1.Name='Test1';
E1.Employee_Last_Name__c ='s';
E1.Employee_ID__c = 'E-12345';
E1.Date_of_Join__c= system.today();
E1.Tax_Regime__c='New Regime';
E1.Confirmed_CTC__c=1500000;
insert E1;
List< Salary_Detail__c>lis= [SELECT Id, Employee_ID__c,Date_of_Joining__c,Department__c,Designation__c,
Gender__c,Location__c,Pan_Number__c,Professional_Tax__c,UAN_No__c,BasicAllowance__c,HouseRentAllowance__c,MonthDays__c,Number_of_Days_present__c, Account_No__c,
Skill_up_Allowance__c,PF__c,SpecialAllowance__c,TotalEarnings__c,TotalDeductions__c,
NetPay__c,In_Words__c FROM Salary_Detail__c ];
Salary_Detail__c s = new Salary_Detail__c();
// s.Employee_Names__c='Tests';
s.Employee_First_Name__c='Test';
s.Employee_Last_Name__c ='s';
s.RecordTypeId = '0123K0000009lfpQAA';
s.Record_Status__c='Active';
s.Date_of_Joining__c=system.today();
s.Employee_ID__c = 'E-1234';
s.Employee_Information__c=E.id;
s.MonthDays__c=31;
S.LastMonthLeaves__c=1;
S.LastMonthLops__c=1;
s.Worked_days__c=30;
s.Confirmed_CTC__c=1500000;
s.Basic_Annual__c=450000;
s.HRA_Annual__c=180000;
s.Special_Allowance_Annual__c=809250;
s.Tax_Regime__c='Old Regime';
s.standard_deduction_monthly__c=4167;
s.Professional_Tax__c=200;
s.Medical_Reimbursement_Annual__c=0;
s.Skill_up_Allowance__c =0;
s.Company_Medical_Insurance_Contribution__c =0;
s.zeroto2_5__c=250000;
s.X2_5to5__c=250000;
s.X5to7_5__c=250000;
s.X7_5to10__c=250000;
s.X10to12_5__c=250000;
s.X12_5to15__c=250000;
s.X15Great__c=0;
s.TaxExempt__c=0;
s.TotalDeductions__c=0;
s.TotalEarnings__c=0;
s.NetPay__c=0;
lis.add (s);
upsert lis;
Salary_Detail__c s1 = new Salary_Detail__c();
// s.Employee_Names__c='Tests';
s1.Employee_First_Name__c='Test';
s1.Employee_Last_Name__c ='s';
s1.RecordTypeId = '0123K0000009lfpQAA';
s1.Record_Status__c='Active';
s1.Date_of_Joining__c=system.today();
s1.Employee_ID__c = 'E-12345';
s1.Employee_Information__c=E1.id;
s1.MonthDays__c=31;
S1.LastMonthLeaves__c=1;
S1.LastMonthLops__c=1;
s1.Worked_days__c=30;
s1.Confirmed_CTC__c=-1;
s1.Basic_Annual__c=450000;
s1.HRA_Annual__c=180000;
s1.Special_Allowance_Annual__c=809250;
s1.Tax_Regime__c='Old Regime';
s1.standard_deduction_monthly__c=4167;
s1.Professional_Tax__c=200;
s1.Medical_Reimbursement_Annual__c=0;
s1.Skill_up_Allowance__c =0;
s1.Company_Medical_Insurance_Contribution__c =0;
s1.zeroto2_5__c=250000;
s1.X2_5to5__c=250000;
s1.X5to7_5__c=250000;
s1.X7_5to10__c=250000;
s1.X10to12_5__c=250000;
s1.X12_5to15__c=250000;
s1.X15Great__c=0;
s1.TaxExempt__c=0;
s1.TotalDeductions__c=0;
s1.TotalEarnings__c=0;
s1.NetPay__c=0;
lis.add (s1);
upsert lis;
Leave_Request__c L = new Leave_Request__c();
L.Approval_Status__c ='Approved';
L.Employee_ID__c = 'E-1234';
L.Employee_Name__c=E.id;
L.Last_Month_LOPs__c=1;
L.Last_Month_Leaves__c=1;
insert L;
//salarydetailtest t = new salarydetailtest();
// salarydetailtest.Salary();
Test.stopTest();
}
}
Thanks in advance
can anyone guide me?
Apex class:
public class salarydetail{
public static list<Salary_Detail__c>salary;
//salary = new list<Salary_Detail__c>();
public String salaryId {
get{
if(salaryId == null && ApexPages.currentPage().getParameters().get('id') != null){
salaryId = ApexPages.currentPage().getParameters().get('id');
}
return salaryId;
}
set;
}
public Salary_Detail__c sd {
get{
return [SELECT Id, Employee_ID__c,Date_of_Joining__c,Department__c,Designation__c,
Gender__c,Location__c,Pan_Number__c,Professional_Tax__c,UAN_No__c,BasicAllowance__c,HouseRentAllowance__c,MonthDays__c,Number_of_Days_present__c, Account_No__c,
Skill_up_Allowance__c,PF__c,SpecialAllowance__c,TotalEarnings__c,TotalDeductions__c,
NetPay__c,In_Words__c FROM Salary_Detail__c WHERE Id = :salaryId LIMIT 1];
}
set;
}
}
Test class:
@ IsTest
public class salarydetailtest {
public static Testmethod void Salary(){
Test.startTest();
Employee_Information__c E = new Employee_Information__c();
E.Name='Test';
E.Employee_Last_Name__c ='s';
E.Employee_ID__c = 'E-1234';
E.Date_of_Join__c= system.today();
E.Tax_Regime__c='Old Regime';
E.Confirmed_CTC__c=1500000;
insert E;
Employee_Information__c E1 = new Employee_Information__c();
E1.Name='Test1';
E1.Employee_Last_Name__c ='s';
E1.Employee_ID__c = 'E-12345';
E1.Date_of_Join__c= system.today();
E1.Tax_Regime__c='New Regime';
E1.Confirmed_CTC__c=1500000;
insert E1;
List< Salary_Detail__c>lis= [SELECT Id, Employee_ID__c,Date_of_Joining__c,Department__c,Designation__c,
Gender__c,Location__c,Pan_Number__c,Professional_Tax__c,UAN_No__c,BasicAllowance__c,HouseRentAllowance__c,MonthDays__c,Number_of_Days_present__c, Account_No__c,
Skill_up_Allowance__c,PF__c,SpecialAllowance__c,TotalEarnings__c,TotalDeductions__c,
NetPay__c,In_Words__c FROM Salary_Detail__c ];
Salary_Detail__c s = new Salary_Detail__c();
// s.Employee_Names__c='Tests';
s.Employee_First_Name__c='Test';
s.Employee_Last_Name__c ='s';
s.RecordTypeId = '0123K0000009lfpQAA';
s.Record_Status__c='Active';
s.Date_of_Joining__c=system.today();
s.Employee_ID__c = 'E-1234';
s.Employee_Information__c=E.id;
s.MonthDays__c=31;
S.LastMonthLeaves__c=1;
S.LastMonthLops__c=1;
s.Worked_days__c=30;
s.Confirmed_CTC__c=1500000;
s.Basic_Annual__c=450000;
s.HRA_Annual__c=180000;
s.Special_Allowance_Annual__c=809250;
s.Tax_Regime__c='Old Regime';
s.standard_deduction_monthly__c=4167;
s.Professional_Tax__c=200;
s.Medical_Reimbursement_Annual__c=0;
s.Skill_up_Allowance__c =0;
s.Company_Medical_Insurance_Contribution__c =0;
s.zeroto2_5__c=250000;
s.X2_5to5__c=250000;
s.X5to7_5__c=250000;
s.X7_5to10__c=250000;
s.X10to12_5__c=250000;
s.X12_5to15__c=250000;
s.X15Great__c=0;
s.TaxExempt__c=0;
s.TotalDeductions__c=0;
s.TotalEarnings__c=0;
s.NetPay__c=0;
lis.add (s);
upsert lis;
Salary_Detail__c s1 = new Salary_Detail__c();
// s.Employee_Names__c='Tests';
s1.Employee_First_Name__c='Test';
s1.Employee_Last_Name__c ='s';
s1.RecordTypeId = '0123K0000009lfpQAA';
s1.Record_Status__c='Active';
s1.Date_of_Joining__c=system.today();
s1.Employee_ID__c = 'E-12345';
s1.Employee_Information__c=E1.id;
s1.MonthDays__c=31;
S1.LastMonthLeaves__c=1;
S1.LastMonthLops__c=1;
s1.Worked_days__c=30;
s1.Confirmed_CTC__c=-1;
s1.Basic_Annual__c=450000;
s1.HRA_Annual__c=180000;
s1.Special_Allowance_Annual__c=809250;
s1.Tax_Regime__c='Old Regime';
s1.standard_deduction_monthly__c=4167;
s1.Professional_Tax__c=200;
s1.Medical_Reimbursement_Annual__c=0;
s1.Skill_up_Allowance__c =0;
s1.Company_Medical_Insurance_Contribution__c =0;
s1.zeroto2_5__c=250000;
s1.X2_5to5__c=250000;
s1.X5to7_5__c=250000;
s1.X7_5to10__c=250000;
s1.X10to12_5__c=250000;
s1.X12_5to15__c=250000;
s1.X15Great__c=0;
s1.TaxExempt__c=0;
s1.TotalDeductions__c=0;
s1.TotalEarnings__c=0;
s1.NetPay__c=0;
lis.add (s1);
upsert lis;
Leave_Request__c L = new Leave_Request__c();
L.Approval_Status__c ='Approved';
L.Employee_ID__c = 'E-1234';
L.Employee_Name__c=E.id;
L.Last_Month_LOPs__c=1;
L.Last_Month_Leaves__c=1;
insert L;
//salarydetailtest t = new salarydetailtest();
// salarydetailtest.Salary();
Test.stopTest();
}
}
Thanks in advance
All Answers
What is the error you were getting?
Thanks!!