+ Start a Discussion
Sandeep M 1Sandeep M 1 

How to write test class for a controller with non dml statements

I have a controller without any dml statements. How can i write test class for the controller to improve the coverage area ?
public with sharing class payslipController

public Decimal loandeduction { get; set; }
public Decimal oIncome { get; set; }
public Decimal Asleaves { get; set; }
public Decimal tsleaves { get; set; }
public Decimal Acleaves { get; set; }
public Decimal tcleaves { get; set; }
public Decimal tds { get; set; }
public Decimal tnetsalary { get; set; }
public Decimal tdeduce { get; set; }
public Decimal pt { get; set; }
public Decimal pf { get; set; }
public Decimal tsalaryAmount { get; set; }
public Decimal obAmount { get; set; }
public Decimal caAmount { get; set; }
public Decimal hraAmount { get; set; }
public Decimal deductions { get; set; }
public Decimal bSalary { get; set; }
public Decimal lwp { get; set; }
public Decimal holidays { get; set; }
public Decimal workingDays { get; set; }
public Integer no_days { get; set; }
public String email { get; set; }
public String gender { get; set; }
public String designation { get; set; }
public String doj { get; set; }
public String panNumber { get; set; }
public String AccountNumber { get; set; }
public String empNo { get; set; }
public String empName { get; set; }
public String month{ get; set; }
public String org_website { get; set; }
public String org_email { get; set; }
public String org_Address { get; set; }
public String org_Name { get; set; }
//Getter and Setters for Objects
public Organization__c orgs{get;set;}
public SFDC_Employee__c employee{get;set;}
public Employee_Monthly_Info__c eMonthInfo{get;set;}
public string empNumbertoget{get;set;}
//Constructor for the controller
public payslipController()
{
    empNumbertoget= ApexPages.CurrentPage().getParameters().get('eno');
    Date selectedDate =  Date.today();
    Date firstDate = selectedDate.toStartOfMonth();
    Date lastDate = firstDate.addDays(date.daysInMonth(selectedDate.year() , selectedDate.month())  - 1);
    DateTime d = datetime.now();
    month= d.format('MMMMM/yyyy');
    String monthName= d.format('MMMMM');


    List<AggregateResult> sumLrequest=[Select sum(days_off__c) s,Request_Start_Date__c, Request_End_Date__c  from SFDC_PTO_Request__c where   Request_Start_Date__c> :firstDate  and Request_End_Date__c< : lastdate and employee__r.Employee_ID__c=:empNumberToGet group by Request_End_Date__c,Request_Start_Date__c ];


    //Retrieve all the required data from Organization__c,Employee__c,Bank_Account__c,Employee_Monthly_Info__c (custom objects) Using SOQL
    List<Organization__c> org=[select id,name,address__c,Email__c,website__c from Organization__c where name='Waste Ventures India Private Limited'];

    List<SFDC_Employee__c> emp=[select name,Employee_ID__c,Title__c,Pan_Number__c,Start_Date__c,Email_Address__c,Gender__c,Bank_Account_No__c,Bank_Name__c,available_casual_leaves__C,available_sick_leaves__c  FROM SFDC_Employee__c where Employee_ID__c=:empNumbertoget];

    List<Employee_Monthly_Info__c> eInfo=[select Employee_Monthly_Info__c.employee__R.name,Medical_Allowance__c,Total_Salary_Amount__c,Net_Salary__c,Total_Deduction__c,Other_Income__c,Loan_Deduction__c,HRA_AMount__c,pt__c,pf__c,Conveyance_Allowance_Amount__c,TDS__c,paid_holidays__c,Total_Casual_Leaves__c,Total_Sick_Leaves__c,basic_salary__c,Leave_with_Pay__c,working_days__c from Employee_Monthly_Info__c where Employee_Monthly_Info__c.employee__R.Employee_ID__c=:empNumbertoget];

    //Retrieve Organization related data

    if(org.size()==0)
    {
         System.debug('org is '+org);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Organization details are empty'));
    }

    else
    {
    //Retrieve Organization related data
    orgs=org[0];
    org_Name=orgs.Name;
    org_Address = orgs.Address__c;
    org_email=orgs.email__c;
    org_website=orgs.website__c;

    //Retrieve Employee related data
    employee=emp[0];
    empName = employee.name;
    empNo=employee.Employee_ID__c;
    acleaves=employee.available_casual_leaves__c;
    asleaves=employee.available_sick_leaves__c;
    panNumber =employee.Pan_Number__c;
    Date dat=employee.Start_Date__c;
    doj=String.valueOf(dat);
    designation=employee.Title__c;
    gender=employee.gender__c;
    email=employee.Email_Address__c;
    AccountNumber = employee.Bank_Account_No__c;
    no_days=Date.daysInMonth(d.year(), d.month());

     if(sumLrequest.size() > 0)
    {
       String str = '' + sumLrequest[0].get('s') ;
       lwp= Decimal.ValueOf(str).setScale(0) ;

    }
    if(sumLrequest.size()==null)
    {
       String str = '' + 0;
       lwp= Decimal.ValueOf(str).setScale(0) ;
    }
    //Retrieve Employee monthly Related data
    eMonthInfo=eInfo[0];
   loandeduction =emonthInfo.loan_deduction__c;
    oIncome=eMonthInfo.other_income__c;
    workingDays =eMonthInfo.working_days__c;
   tcleaves=eMonthInfo.total_casual_leaves__c;
    tsleaves=eMonthInfo.total_Sick_leaves__c;
    bSalary=eMonthInfo.basic_salary__c;
    deductions=eMonthInfo.total_deduction__c;
    tsalaryAmount=eMonthInfo.Total_Salary_Amount__c;
    pf=eMonthInfo.pf__c;
    pt=eMonthInfo.pt__c;
    tdeduce=eMonthInfo.total_deduction__c;
    caAmount=eMonthInfo.Conveyance_Allowance_Amount__c;
    hraAmount=eMonthInfo.HRA_Amount__c;
    obAmount=eMonthInfo.Medical_Allowance__c;
    tnetsalary=eMonthInfo.net_salary__c;
    tds=eMonthInfo.tds__c;
    }

}
}
Tony TannousTony Tannous
For sure you have a VF page for your controller ( example EmployeVF)so you can simply have a test class like this one .

@isTest
private class payslipController_TEST {

    static testMethod void TestController()
    {

      // you need to do the insert statement for the data that you want to selectt in the controller.
        Employee__c emp = new Employee__c();
        insert emp;
     
         test.startTest();
         PageReference pageRef = Page.EmployeVF;
         pageRef.getParameters().put('eno', emp.id);
        Test.setCurrentPageReference(pageRef);
         payslipController paysLip= new payslipController();

         test.stopTest();  
}

}

and in case you have other functionality in the controller you can simply invoke them after intiating the constuctor.

Good Luck
Sandeep M 1Sandeep M 1
@Tony Thanks for quick response, I am using like below code , The test methods are not passing and coverage area is 0% only

@isTest
private class payslipController_TEST {

    static testMethod void TestController()
    {

        SFDC_Employee__c emp = new SFDC_Employee__c();
        insert emp;
     
         test.startTest();
         PageReference pageRef = Page.PaySlipPage;
         pageRef.getParameters().put('eno', '0000613');
        Test.setCurrentPageReference(pageRef);
         payslipController paysLip= new payslipController();

         test.stopTest();  
}

}
Tony TannousTony Tannous
Try the below :

1- if there is a try catch in the test method class remove them ( and run the test class).

2- for testing purpose use the seealldata so you can test with the current data without doing insert statement in test class( may be there is a trigger that is generating the problem).

3- if it doesn't work post the test class as is so i can check it for you.

Regards

Sandeep M 1Sandeep M 1
Thanks for that ! I already posted my test class that i am using now :) I will try it using seeAlldata now