You need to sign in to do that
Don't have an account?
Dastagiri Basha
Hii all, need code coverage for this apex class, Can any one Help me.
This is my apex Class
public class employeeDateSearch {
@AuraEnabled
public static List<Employee_Payroll__c> getEmployeeData(string monthYear){
List<Employee_Payroll__c> returnList = new List<Employee_Payroll__c>();
Set<Id> empIds = new Set<Id>();
List<Employee_Payroll__c> empl = [SELECT Id, Name__c, Attendence__c,Employee_Name__c, Allowances__c, Holidays__c, Advance__c, Visa__c, Iqama__c, Insurance__c, Ticket__c, Others__c, Start_Date__c, End_Date__c, Ot__c, Sick_Leave__c, Basic_Salary__c FROM Employee_Payroll__c WHERE Month_and_year__c=:monthYear];
if(empl.size() >0){
for(Employee_Payroll__c epl : empl){
empIds.add(epl.Name__c);
returnList.add(epl);
}
}
List<Employee__c> empList;
AggregateResult[] atteList;
AggregateResult[] LeaveList;
if(empIds.size() >0){
empList = [SELECT Id,Name,Basic_Salary__c,Employee_ID__c FROM Employee__c WHERE Not (Id IN :empIds)];
atteList = [SELECT Count(Id)cnt,Employee__c FROM Attendance__c WHERE Month_and_year__c=:monthYear AND (NOT (Employee__c IN : empIds)) GROUP BY Employee__c];
LeaveList = [SELECT SUM(No_of_Days__c)noL,Employee__c FROM Leave__c WHERE Month_and_year__c=:monthYear AND (NOT (Employee__c IN : empIds)) AND (Approval_Status__c ='Accept' OR Approval_Status__c ='Edit & Accept') GROUP BY Employee__c];
}else{
empList = [SELECT Id,Name,Basic_Salary__c,Employee_ID__c FROM Employee__c];
atteList = [SELECT Count(Id)cnt,Employee__c FROM Attendance__c WHERE Month_and_year__c=:monthYear GROUP BY Employee__c];
LeaveList = [SELECT SUM(No_of_Days__c)noL,Employee__c FROM Leave__c WHERE Month_and_year__c=:monthYear AND (Approval_Status__c ='Accept' OR Approval_Status__c ='Edit & Accept') GROUP BY Employee__c];
}
for(Employee__c est : empList){
Employee_Payroll__c epll = new Employee_Payroll__c();
epll.Name__c = est.Id;
epll.Basic_Salary__c = est.Basic_Salary__c;
epll.Employee_Name__c = est.Name;
for(AggregateResult agat : atteList){
if((id)agat.get('Employee__c') == est.Id){
epll.Attendence__c = (decimal)agat.get('cnt');
}
}
for(AggregateResult agls : LeaveList){
if((id)agls.get('Employee__c') == est.Id){
epll.Sick_Leave__c = (decimal)agls.get('noL');
}
}
returnList.add(epll);
}
return returnList;
}
@AuraEnabled
public static List<Employee_Payroll__c> savePayroll( List<Employee_Payroll__c> emplList, string monthName, string yearName){
List<Employee_Payroll__c> updateList = new List<Employee_Payroll__c>();
for(Employee_Payroll__c empl : emplList){
Employee_Payroll__c epl = new Employee_Payroll__c();
if(empl.Id !=null ){
epl.Id = empl.Id;
}
epl.Month__c = monthName;
epl.Year__c = yearName;
epl.Name__c = empl.Name__c;
epl.Basic_Salary__c = empl.Basic_Salary__c;
epl.Ot__c = empl.Ot__c;
epl.Attendence__c = empl.Attendence__c;
epl.Sick_Leave__c = empl.Sick_Leave__c;
epl.Advance__c = empl.Advance__c;
epl.Visa__c = empl.Visa__c;
epl.Iqama__c = empl.Iqama__c;
epl.Insurance__c = empl.Insurance__c;
epl.Ticket__c = empl.Ticket__c;
updateList.add(epl);
}
upsert updateList;
return updateList;
}
This is my Test Class::
@isTest
public class employeeDateSearchTest {
public static testMethod void getEmployeeDataTest(){
Test.startTest();
Set<Id> empIds = new Set<Id>();
List<Employee_Payroll__c> UpPayroll = new List<Employee_Payroll__c>();
Employee_Payroll__c empPayroll = new Employee_Payroll__c();
empPayroll.Name__c = 'a0h1s000001uiHZAAY';
empPayroll.Basic_Salary__c = 2000;
empPayroll.Ot__c = 2;
empPayroll.Attendence__c = 23;
empPayroll.Sick_Leave__c = 2;
empPayroll.Advance__c = 200;
empPayroll.Visa__c = 250;
empPayroll.Iqama__c = 300;
empPayroll.Insurance__c = 100;
empPayroll.Ticket__c = 200;
empPayroll.Holidays__c =4;
empPayroll.Allowances__c=400;
empPayroll.Others__c= 200;
empIds.add(empPayroll.Name__c);
//empPayroll.Month_and_year__c='' ;
UpPayroll.add(empPayroll);
insert UpPayroll;
employeeDateSearch.getEmployeeData('system.today');
test.stopTest();
}
public static testMethod void SavePayrollTest(){
test.startTest();
List<Employee_Payroll__c> updList = new List<Employee_Payroll__c>();
Employee_Payroll__c emproll = new Employee_Payroll__c();
// emproll.id = empList.id;
// emproll.Month__c = 'November';
// emproll.Year__c = '2021';
emproll.Name__c = 'a0h1s000001uiHZAAY';
emproll.Basic_Salary__c = 2000;
emproll.Ot__c = 2;
emproll.Attendence__c = 23;
emproll.Sick_Leave__c = 2;
emproll.Advance__c = 200;
emproll.Visa__c = 250;
emproll.Iqama__c = 300;
emproll.Insurance__c = 100;
emproll.Ticket__c = 200;
updList.add(emproll);
upsert updList;
employeeDateSearch.savePayroll( updList, 'November', '2021');
test.stopTest();
}
}
With this Test class i have coverd 60% and I am a Begginer Can any one help me with the Test Class.
public class employeeDateSearch {
@AuraEnabled
public static List<Employee_Payroll__c> getEmployeeData(string monthYear){
List<Employee_Payroll__c> returnList = new List<Employee_Payroll__c>();
Set<Id> empIds = new Set<Id>();
List<Employee_Payroll__c> empl = [SELECT Id, Name__c, Attendence__c,Employee_Name__c, Allowances__c, Holidays__c, Advance__c, Visa__c, Iqama__c, Insurance__c, Ticket__c, Others__c, Start_Date__c, End_Date__c, Ot__c, Sick_Leave__c, Basic_Salary__c FROM Employee_Payroll__c WHERE Month_and_year__c=:monthYear];
if(empl.size() >0){
for(Employee_Payroll__c epl : empl){
empIds.add(epl.Name__c);
returnList.add(epl);
}
}
List<Employee__c> empList;
AggregateResult[] atteList;
AggregateResult[] LeaveList;
if(empIds.size() >0){
empList = [SELECT Id,Name,Basic_Salary__c,Employee_ID__c FROM Employee__c WHERE Not (Id IN :empIds)];
atteList = [SELECT Count(Id)cnt,Employee__c FROM Attendance__c WHERE Month_and_year__c=:monthYear AND (NOT (Employee__c IN : empIds)) GROUP BY Employee__c];
LeaveList = [SELECT SUM(No_of_Days__c)noL,Employee__c FROM Leave__c WHERE Month_and_year__c=:monthYear AND (NOT (Employee__c IN : empIds)) AND (Approval_Status__c ='Accept' OR Approval_Status__c ='Edit & Accept') GROUP BY Employee__c];
}else{
empList = [SELECT Id,Name,Basic_Salary__c,Employee_ID__c FROM Employee__c];
atteList = [SELECT Count(Id)cnt,Employee__c FROM Attendance__c WHERE Month_and_year__c=:monthYear GROUP BY Employee__c];
LeaveList = [SELECT SUM(No_of_Days__c)noL,Employee__c FROM Leave__c WHERE Month_and_year__c=:monthYear AND (Approval_Status__c ='Accept' OR Approval_Status__c ='Edit & Accept') GROUP BY Employee__c];
}
for(Employee__c est : empList){
Employee_Payroll__c epll = new Employee_Payroll__c();
epll.Name__c = est.Id;
epll.Basic_Salary__c = est.Basic_Salary__c;
epll.Employee_Name__c = est.Name;
for(AggregateResult agat : atteList){
if((id)agat.get('Employee__c') == est.Id){
epll.Attendence__c = (decimal)agat.get('cnt');
}
}
for(AggregateResult agls : LeaveList){
if((id)agls.get('Employee__c') == est.Id){
epll.Sick_Leave__c = (decimal)agls.get('noL');
}
}
returnList.add(epll);
}
return returnList;
}
@AuraEnabled
public static List<Employee_Payroll__c> savePayroll( List<Employee_Payroll__c> emplList, string monthName, string yearName){
List<Employee_Payroll__c> updateList = new List<Employee_Payroll__c>();
for(Employee_Payroll__c empl : emplList){
Employee_Payroll__c epl = new Employee_Payroll__c();
if(empl.Id !=null ){
epl.Id = empl.Id;
}
epl.Month__c = monthName;
epl.Year__c = yearName;
epl.Name__c = empl.Name__c;
epl.Basic_Salary__c = empl.Basic_Salary__c;
epl.Ot__c = empl.Ot__c;
epl.Attendence__c = empl.Attendence__c;
epl.Sick_Leave__c = empl.Sick_Leave__c;
epl.Advance__c = empl.Advance__c;
epl.Visa__c = empl.Visa__c;
epl.Iqama__c = empl.Iqama__c;
epl.Insurance__c = empl.Insurance__c;
epl.Ticket__c = empl.Ticket__c;
updateList.add(epl);
}
upsert updateList;
return updateList;
}
This is my Test Class::
@isTest
public class employeeDateSearchTest {
public static testMethod void getEmployeeDataTest(){
Test.startTest();
Set<Id> empIds = new Set<Id>();
List<Employee_Payroll__c> UpPayroll = new List<Employee_Payroll__c>();
Employee_Payroll__c empPayroll = new Employee_Payroll__c();
empPayroll.Name__c = 'a0h1s000001uiHZAAY';
empPayroll.Basic_Salary__c = 2000;
empPayroll.Ot__c = 2;
empPayroll.Attendence__c = 23;
empPayroll.Sick_Leave__c = 2;
empPayroll.Advance__c = 200;
empPayroll.Visa__c = 250;
empPayroll.Iqama__c = 300;
empPayroll.Insurance__c = 100;
empPayroll.Ticket__c = 200;
empPayroll.Holidays__c =4;
empPayroll.Allowances__c=400;
empPayroll.Others__c= 200;
empIds.add(empPayroll.Name__c);
//empPayroll.Month_and_year__c='' ;
UpPayroll.add(empPayroll);
insert UpPayroll;
employeeDateSearch.getEmployeeData('system.today');
test.stopTest();
}
public static testMethod void SavePayrollTest(){
test.startTest();
List<Employee_Payroll__c> updList = new List<Employee_Payroll__c>();
Employee_Payroll__c emproll = new Employee_Payroll__c();
// emproll.id = empList.id;
// emproll.Month__c = 'November';
// emproll.Year__c = '2021';
emproll.Name__c = 'a0h1s000001uiHZAAY';
emproll.Basic_Salary__c = 2000;
emproll.Ot__c = 2;
emproll.Attendence__c = 23;
emproll.Sick_Leave__c = 2;
emproll.Advance__c = 200;
emproll.Visa__c = 250;
emproll.Iqama__c = 300;
emproll.Insurance__c = 100;
emproll.Ticket__c = 200;
updList.add(emproll);
upsert updList;
employeeDateSearch.savePayroll( updList, 'November', '2021');
test.stopTest();
}
}
With this Test class i have coverd 60% and I am a Begginer Can any one help me with the Test Class.
You are missing Employee__c & Attendance__c objects test data in the test class.
If this helps, please mark it best answer.
Regards,
Ankaiah
Below test class is giving 92%. Please let me know if you need any clarifications. If this solution helps, please mark it best answer.
Thanks,
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!