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

         System.debug('org is '+org);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Organization details are empty'));

    //Retrieve Organization related data
    org_Address = orgs.Address__c;

    //Retrieve Employee related data
    empName = employee.name;
    panNumber =employee.Pan_Number__c;
    Date dat=employee.Start_Date__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) ;

       String str = '' + 0;
       lwp= Decimal.ValueOf(str).setScale(0) ;
    //Retrieve Employee monthly Related data
   loandeduction =emonthInfo.loan_deduction__c;
    workingDays =eMonthInfo.working_days__c;


The point of the test classes is to test your logic, not necessarily any DML statements. In your case, there are logic conditions you'd want to test. There are a few ways you can test this, but the most straight forward is to create a test method for each condition.

i.e. create a test method where org.size() == 0, another where sumLrequest.size() == null and another where sumLrequest.size() > 0.

To test the first Organization__c if statement, you could use something similar to the following:

static testMethod void testNoOrg() {
    payslipController psc ;

    // Ensure there are no page messages before calling constructor
    system.assert(!ApexPages.hasMessages(ApexPages.Severity.FATAL)) ;

    psc = new payslipController() ;

    system.assert(ApexPages.hasMessages(ApexPages.Severity.FATAL)) ;

(This test method hasn't been tested, so please update if it doesn't work.)

The idea behind this simple test method is that you haven't set up any data, so there won't be any Organization__c records in the database. For the other tests, you'll need to create test Organization__c and SFDC_PTO_Request__c records so you can test the if statements. It makes sense to create a variety of records to test your SOQL query with differing start and end dates.

  • 1
    But not sumLrequest.size() == null, because it can't be.
    – sfdcfox
    Jun 30 '14 at 4:49
  • yeah, I couldn't remember off the top of my head whether it would handle that, but obviously this is a bonus of the testing framework. As the user tries to implement this logic in a test class, they'll see which parts of their code will behave the way they expect.
    – Nick Cook
    Jun 30 '14 at 4:51
  • @NickCook can you give it some sample to refer the above scenario ? Jun 30 '14 at 5:23
  • 1
    The point here is that sometimes code that is untestable can't be tested because it represents an impossible situation; usually this means removing that code or revising. Most (all?) library functions that return a Boolean or Integer value will never return null. E.g. if(String.isBlank(someString) == null) will never be true. The few functions that can return null typically do so because it makes sense. Size() would never be null, because a collection always knows its size. Also, queries never return null, but can return an empty list.
    – sfdcfox
    Jun 30 '14 at 5:36
  • Yep. Hopefully as Gajini tries to write the test method to cover the sumLrequest.size() == null scenario, he'll realise it won't evaluate as he expects. This should hopefully help him fix up that part of his code.
    – Nick Cook
    Jun 30 '14 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.