+ Start a Discussion
Neeraj Sharma 103Neeraj Sharma 103 

In below Apex class my test class code covered is 55%

Hi Please help for test class

public with sharing class schedulexpensecontroller {

     public list<Expense_Management__c> con{get;set;}  
     public list<Expense_Management__c> em{get;set;}  
     public double total1{get;set;} 
     public Date fromdate{get;set;}
     public Date todate{get;set;}
     
	 public Id parent {get;set;} 
    
     public schedulexpensecontroller(){
    	
        
  	    Id conId = apexpages.currentpage().getparameters().get('id');
        parent = UserInfo.getUserId();
         
 			
                     
          
        if(apexpages.currentpage().getparameters().get('fromdate') !=null && apexpages.currentpage().getparameters().get('todate')!=null)
        {
             fromdate = Date.Parse(apexpages.currentpage().getparameters().get('fromdate'));
             todate = Date.Parse(apexpages.currentpage().getparameters().get('todate'));
            
            con = [Select Date__c,From__c,To__c,Purpose_of_Travel__c,Mode_of_Travel__c,
                  Opening_KM__c,Closing_KM__c,Rate__c,Total_Km__c,Total_Travel_Expense__c	,
                  Lodging_Expense__c,Miscellaneous_Expense__c,
                  Description__c,Total_Expenses__c,Local_Conveyance__c,	Paid_Amount__c,Payment__c,
                  Name_of_Employee__r.EmployeeNumber,Name_of_Employee__r.UserRole.Name,Name_of_Employee__r.Name from Expense_Management__c
                  where Name_of_Employee__c =: parent AND CreatedDate >=: fromdate AND CreatedDate <=: todate];
            
            em = [Select Payment__c,Total_Expenses__c from Expense_Management__c where Payment__c = 'unpaid' AND Name_of_Employee__c =: parent];  
        }else
        {
            
              con = [Select Date__c,From__c,To__c,Purpose_of_Travel__c,Mode_of_Travel__c,
                  Opening_KM__c,Closing_KM__c,Rate__c,Total_Km__c,Total_Travel_Expense__c	,
                  Lodging_Expense__c,Miscellaneous_Expense__c,
                  Description__c,Total_Expenses__c,Local_Conveyance__c,	Paid_Amount__c,Payment__c,
                  Name_of_Employee__r.EmployeeNumber,Name_of_Employee__r.UserRole.Name,Name_of_Employee__r.Name from Expense_Management__c
                  where Name_of_Employee__c =: conId ];
            
              em = [Select Payment__c,Total_Expenses__c from Expense_Management__c where Payment__c = 'unpaid' AND Name_of_Employee__c =: conId];
        }
        
    }
}
@isTest
public class Testclassforscheduleexpensecontroller {

    @isTest static void testmethod1()
    {
        schedulexpensecontroller cons= new schedulexpensecontroller();
          // Date from1;
      // cons.fromdate=from1.parse('06/18/2019');
        
       
       //fromdate = Date.Parse(apexpages.currentpage().getparameters().get('fromdate'));
    }
}


 
Best Answer chosen by Neeraj Sharma 103
Ajay K DubediAjay K Dubedi
Hi Neeraj,

Try the following test class,it may be helpful for you:
@isTest
public class Testclassforscheduleexpensecontroller {

    @isTest static void testmethod1()
    {
        Name_of_Employee__r empName=new Name_of_Employee__r();
        empName.EmployeeNumber=12345;
        empName.UserRole.Name='Admin';
        empName.Name='test';
        Insert empName;
    
        Expense_Management__c expenseMgnmt=new Expense_Management__c();
        expenseMgnmt.Name_of_Employee__r=empName;
        expenseMgnmt.Date__c=system.today();
        expenseMgnmt.From__c=system.today();
        expenseMgnmt.To__c='test';
        expenseMgnmt.Purpose_of_Travel__c='purposeTest';
        expenseMgnmt.Mode_of_Travel__c='Car';
        expenseMgnmt.Opening_KM__c=10;
        expenseMgnmt.Closing_KM__c=100;
        expenseMgnmt.Rate__c=25;
        expenseMgnmt.Total_Km__c=110;
        expenseMgnmt.Total_Travel_Expense__c=1500;
        expenseMgnmt.Lodging_Expense__c=200;
        expenseMgnmt.Miscellaneous_Expense__c=500;
        expenseMgnmt.Description__c='Travelling';
        expenseMgnmt.Total_Expenses__c=5000;
        expenseMgnmt.Local_Conveyance__c=120;
        expenseMgnmt.Paid_Amount__c=225;
        expenseMgnmt.Payment__c='unpaid';
        Insert expenseMgnmt;
        
       Date startDate = date.newInstance(2019, 6, 15);
       Date endDate = date.newInstance(2019, 7, 15); 
       
       test.startTest();
       PageReference pageRef = Page.VFPageName;
       Test.setCurrentPage(pageRef);
       ApexPages.currentPage().getParameters().put('fromdate',startDate);  ApexPages.currentPage().getParameters().put('todate',endDate); 
       ApexPages.currentPage().getParameters().put('id',empName.Id);

       schedulexpensecontroller cons= new schedulexpensecontroller();
       test.stopTest();
    }
    @isTest static void testmethod2()
    {
        Name_of_Employee__r empName=new Name_of_Employee__r();
        empName.EmployeeNumber=12345;
        empName.UserRole.Name='Admin';
        empName.Name='test';
        Insert empName;
    
        Expense_Management__c expenseMgnmt=new Expense_Management__c();
        expenseMgnmt.Name_of_Employee__r=empName;
        expenseMgnmt.Date__c=system.today();
        expenseMgnmt.From__c=system.today();
        expenseMgnmt.To__c='test';
        expenseMgnmt.Purpose_of_Travel__c='purposeTest';
        expenseMgnmt.Mode_of_Travel__c='Car';
        expenseMgnmt.Opening_KM__c=10;
        expenseMgnmt.Closing_KM__c=100;
        expenseMgnmt.Rate__c=25;
        expenseMgnmt.Total_Km__c=110;
        expenseMgnmt.Total_Travel_Expense__c=1500;
        expenseMgnmt.Lodging_Expense__c=200;
        expenseMgnmt.Miscellaneous_Expense__c=500;
        expenseMgnmt.Description__c='Travelling';
        expenseMgnmt.Total_Expenses__c=5000;
        expenseMgnmt.Local_Conveyance__c=120;
        expenseMgnmt.Paid_Amount__c=225;
        expenseMgnmt.Payment__c='unpaid';
        Insert expenseMgnmt;
        
        test.startTest();
        PageReference pageRef = Page.VFPageName;
        Test.setCurrentPage(pageRef); 
        ApexPages.currentPage().getParameters().put('id',empName.Id);

        schedulexpensecontroller cons= new schedulexpensecontroller();
        test.stopTest();
    }
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Neeraj,

Here is the dummy data which I am providing in this test class, the custom fields which you are using in your SOQL query you need to create records for those custom fields by using the same you can cover in your class:

@isTest
public class Testclassforscheduleexpensecontroller {

    @isTest static void testmethod1()
    {
        Name_of_Employee__r empName=new Name_of_Employee__r();
        empName.EmployeeNumber=12345;
        empName.UserRole.Name='Admin';
        empName.Name='test';
        Insert empName;
    
        Expense_Management__c expenseMgnmt=new Expense_Management__c();
        expenseMgnmt.Name_of_Employee__r=empName;
        expenseMgnmt.Date__c=system.today();
        expenseMgnmt.From__c=system.today();
        expenseMgnmt.To__c='test';
        expenseMgnmt.Purpose_of_Travel__c='purposeTest';
        expenseMgnmt.Mode_of_Travel__c='Car';
        expenseMgnmt.Opening_KM__c=10;
        expenseMgnmt.Closing_KM__c=100;
        expenseMgnmt.Rate__c=25;
        expenseMgnmt.Total_Km__c=110;
        expenseMgnmt.Total_Travel_Expense__c=1500;
        expenseMgnmt.Lodging_Expense__c=200;
        expenseMgnmt.Miscellaneous_Expense__c=500;
        expenseMgnmt.Description__c='Travelling';
        expenseMgnmt.Total_Expenses__c=5000;
        expenseMgnmt.Local_Conveyance__c=120;
        expenseMgnmt.Paid_Amount__c=225;
        expenseMgnmt.Payment__c='Completed';
        Insert expenseMgnmt;
        
       Date startDate = date.newInstance(2019, 6, 15);
       Date endDate = date.newInstance(2019, 7, 15); 
        
       PageReference pageRef = Page.PageName;
       ApexPages.currentPage().getParameters().put('fromdate',startDate);  ApexPages.currentPage().getParameters().put('todate',endDate); 
        
       schedulexpensecontroller cons= new schedulexpensecontroller();
       Test.setCurrentPage(pageRef);
    }
    @isTest static void testmethod1()
    {
        Name_of_Employee__r empName=new Name_of_Employee__r();
        empName.EmployeeNumber=12345;
        empName.UserRole.Name='Admin';
        empName.Name='test';
        Insert empName;
    
        Expense_Management__c expenseMgnmt=new Expense_Management__c();
        expenseMgnmt.Name_of_Employee__r=empName;
        expenseMgnmt.Date__c=system.today();
        expenseMgnmt.From__c=system.today();
        expenseMgnmt.To__c='test';
        expenseMgnmt.Purpose_of_Travel__c='purposeTest';
        expenseMgnmt.Mode_of_Travel__c='Car';
        expenseMgnmt.Opening_KM__c=10;
        expenseMgnmt.Closing_KM__c=100;
        expenseMgnmt.Rate__c=25;
        expenseMgnmt.Total_Km__c=110;
        expenseMgnmt.Total_Travel_Expense__c=1500;
        expenseMgnmt.Lodging_Expense__c=200;
        expenseMgnmt.Miscellaneous_Expense__c=500;
        expenseMgnmt.Description__c='Travelling';
        expenseMgnmt.Total_Expenses__c=5000;
        expenseMgnmt.Local_Conveyance__c=120;
        expenseMgnmt.Paid_Amount__c=225;
        expenseMgnmt.Payment__c='Completed';
        Insert expenseMgnmt;
    
       schedulexpensecontroller cons= new schedulexpensecontroller();
    }
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
 
Neeraj Sharma 103Neeraj Sharma 103
Hi 
Ajay K Dubedi

its cover 66% Code Coverage
Neeraj Sharma 103Neeraj Sharma 103
Hi Ajay K Dubedi
 
fromdate = Date.Parse(apexpages.currentpage().getparameters().get('fromdate'));
             todate = Date.Parse(apexpages.currentpage().getparameters().get('todate'));
            
            con = [Select Date__c,From__c,To__c,Purpose_of_Travel__c,Mode_of_Travel__c,
                  Opening_KM__c,Closing_KM__c,Rate__c,Total_Km__c,Total_Travel_Expense__c	,
                  Lodging_Expense__c,Miscellaneous_Expense__c,
                  Description__c,Total_Expenses__c,Local_Conveyance__c,	Paid_Amount__c,Payment__c,
                  Name_of_Employee__r.EmployeeNumber,Name_of_Employee__r.UserRole.Name,Name_of_Employee__r.Name from Expense_Management__c
                  where Name_of_Employee__c =: parent AND CreatedDate >=: fromdate AND CreatedDate <=: todate];
            
            em = [Select Payment__c,Total_Expenses__c from Expense_Management__c where Payment__c = 'unpaid' AND Name_of_Employee__c =: parent];

Above part is not covered in test class
Ajay K DubediAjay K Dubedi
Hi Neeraj,

Kindly share with me whatever error and exception you are getting.So that I can look into it and do share with us the screen-shot of class code coverage:

Thanks,
Ajay Dubedi
Neeraj Sharma 103Neeraj Sharma 103
User-added imageUser-added image
Neeraj Sharma 103Neeraj Sharma 103
Hi 
Ajay K Dubedi

No error and exception i am not getting when run the test class test class will be run perfectly with green check mark but code coverage is 70%
Ajay K DubediAjay K Dubedi
Hi Neeraj,

Try the following test class,it may be helpful for you:
@isTest
public class Testclassforscheduleexpensecontroller {

    @isTest static void testmethod1()
    {
        Name_of_Employee__r empName=new Name_of_Employee__r();
        empName.EmployeeNumber=12345;
        empName.UserRole.Name='Admin';
        empName.Name='test';
        Insert empName;
    
        Expense_Management__c expenseMgnmt=new Expense_Management__c();
        expenseMgnmt.Name_of_Employee__r=empName;
        expenseMgnmt.Date__c=system.today();
        expenseMgnmt.From__c=system.today();
        expenseMgnmt.To__c='test';
        expenseMgnmt.Purpose_of_Travel__c='purposeTest';
        expenseMgnmt.Mode_of_Travel__c='Car';
        expenseMgnmt.Opening_KM__c=10;
        expenseMgnmt.Closing_KM__c=100;
        expenseMgnmt.Rate__c=25;
        expenseMgnmt.Total_Km__c=110;
        expenseMgnmt.Total_Travel_Expense__c=1500;
        expenseMgnmt.Lodging_Expense__c=200;
        expenseMgnmt.Miscellaneous_Expense__c=500;
        expenseMgnmt.Description__c='Travelling';
        expenseMgnmt.Total_Expenses__c=5000;
        expenseMgnmt.Local_Conveyance__c=120;
        expenseMgnmt.Paid_Amount__c=225;
        expenseMgnmt.Payment__c='unpaid';
        Insert expenseMgnmt;
        
       Date startDate = date.newInstance(2019, 6, 15);
       Date endDate = date.newInstance(2019, 7, 15); 
       
       test.startTest();
       PageReference pageRef = Page.VFPageName;
       Test.setCurrentPage(pageRef);
       ApexPages.currentPage().getParameters().put('fromdate',startDate);  ApexPages.currentPage().getParameters().put('todate',endDate); 
       ApexPages.currentPage().getParameters().put('id',empName.Id);

       schedulexpensecontroller cons= new schedulexpensecontroller();
       test.stopTest();
    }
    @isTest static void testmethod2()
    {
        Name_of_Employee__r empName=new Name_of_Employee__r();
        empName.EmployeeNumber=12345;
        empName.UserRole.Name='Admin';
        empName.Name='test';
        Insert empName;
    
        Expense_Management__c expenseMgnmt=new Expense_Management__c();
        expenseMgnmt.Name_of_Employee__r=empName;
        expenseMgnmt.Date__c=system.today();
        expenseMgnmt.From__c=system.today();
        expenseMgnmt.To__c='test';
        expenseMgnmt.Purpose_of_Travel__c='purposeTest';
        expenseMgnmt.Mode_of_Travel__c='Car';
        expenseMgnmt.Opening_KM__c=10;
        expenseMgnmt.Closing_KM__c=100;
        expenseMgnmt.Rate__c=25;
        expenseMgnmt.Total_Km__c=110;
        expenseMgnmt.Total_Travel_Expense__c=1500;
        expenseMgnmt.Lodging_Expense__c=200;
        expenseMgnmt.Miscellaneous_Expense__c=500;
        expenseMgnmt.Description__c='Travelling';
        expenseMgnmt.Total_Expenses__c=5000;
        expenseMgnmt.Local_Conveyance__c=120;
        expenseMgnmt.Paid_Amount__c=225;
        expenseMgnmt.Payment__c='unpaid';
        Insert expenseMgnmt;
        
        test.startTest();
        PageReference pageRef = Page.VFPageName;
        Test.setCurrentPage(pageRef); 
        ApexPages.currentPage().getParameters().put('id',empName.Id);

        schedulexpensecontroller cons= new schedulexpensecontroller();
        test.stopTest();
    }
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
This was selected as the best answer