+ Start a Discussion
Mohammad AzamMohammad Azam 

Need help with Visualforce Page controller test Class Coverage

Visualforce Page
<apex:page Controller="RoomOccupancyRptCntl" docType="html-5.0">    
    <script language="javascript">
      
       function getResultMonth(param,freq) {
           //alert(freq);
           if(freq=='monthly'){
                         switch (param) {
                      case "JAN":
                          param=1;                        
                        break;
                      case "FEB":
                          param=2;                         
                        break;
                      case "MAR":
                          param=3;                         
                        break;
                      case "APR":
                          param=4;                         
                        break;                     
                      case "MAY":
                          param=5;                         
                        break;
                        case "JUN":
                          param=6;                         
                        break;
                        case "JUL":
                          param=7;                         
                        break;
                        case "AUG":
                          param=8;                         
                        break;
                        case "SEP":
                          param=9;                         
                        break;
                        case "OCT":
                          param=10;                         
                        break;
                        case "NOV":
                          param=11;                         
                        break;
                        case "DEC":
                          param=12;                         
                        break;
                                              
                    } 
                    additionalFilter(param); 
           }
           
         else{
         additionalFilter(param);
         }
           
           
       }
    </script>    
<apex:form >
        <apex:actionFunction name="additionalFilter" action="{!additionalFilter}" reRender="out">
                    
       <apex:pageBlock title="Additional Filter" id="out" >
     
       <apex:pageBlockSection columns="1">
          <apex:pageBlockTable value="{!subBookings}" var="booking" >         
             <apex:column value="{!booking.Account__c}"/>
             <apex:column value="{!booking.Contact__c}"/>
             <apex:column value="{!booking.Virtual_Office_Type__c}"/>
             <apex:column value="{!booking.M__c}"/>
             <apex:column value="{!booking.Office_No__c}"/>
             <apex:column value="{!booking.Meeting_Room_No__c}"/>
             <apex:column value="{!booking.Actual_Start_Date_Time__c}"/>
             <apex:column value="{!booking.End_Date_Time__c}"/>
          </apex:pageBlockTable>
          </apex:pageBlockSection>
       </apex:pageBlock>
      

   </apex:form>   
</apex:page>

Controller 
 
public class RoomOccupancyRptCntl {


 public List < My_Sales__c > subBookings {
  get;
  set;
 }



 public PageReference additionalFilter() {
  integer selectedMonth = integer.valueOf(Apexpages.currentPage().getParameters().get('Param'));
  List < My_Sales__c > jan15 = new List < My_Sales__c > ();
  List < My_Sales__c > year16 = new List < My_Sales__c > ();
  List < My_Sales__c > year17 = new List < My_Sales__c > ();
  List < My_Sales__c > year18 = new List < My_Sales__c > ();
  List < My_Sales__c > year19 = new List < My_Sales__c > ();
  List < My_Sales__c > year20 = new List < My_Sales__c > ();
  subBookings = new List < My_Sales__c > ();
  Integer year = 2015;
  if (selyear != 'None')
   year = Integer.valueof(selyear);
  Integer endYear = year + 5;

  jan15 = [Select Contact_Name__c, Contact__c, Account__c, Virtual_Office_Type__c, M__c, Office_No__c,
   Meeting_Room_No__c, Actual_Start_Date_Time__c, End_Date_Time__c, Active__c, X2015_January__c,
   X2015_February__c, X2015_March__c, X2015_April__c, X2015_May__c, X2015_June__c, X2015_July__c,
   X2015_August__c, X2015_September__c, X2015_October__c, X2015_November__c, X2015_December__c
   FROM My_Sales__c
  ];

  year16 = [Select Contact_Name__c, Contact__c, Account__c, Virtual_Office_Type__c, M__c, Office_No__c,
   Meeting_Room_No__c, Actual_Start_Date_Time__c, End_Date_Time__c, Active__c, X2016_January__c,
   X2016_February__c, X2016_March__c, X2016_April__c, X2016_May__c, X2016_June__c, X2016_July__c,
   X2016_August__c, X2016_September__c, X2016_October__c, X2016_November__c, X2016_December__c
   FROM My_Sales__c
  ];

  year17 = [Select Contact_Name__c, Contact__c, Account__c, Virtual_Office_Type__c, M__c, Office_No__c,
   Meeting_Room_No__c, Actual_Start_Date_Time__c, End_Date_Time__c, Active__c, X2017_January__c,
   X2017_February__c, X2017_March__c, X2017_April__c, X2017_May__c, X2017_June__c, X2017_July__c,
   X2017_August__c, X2017_September__c, X2017_October__c, X2017_November__c, X2017_December__c
   FROM My_Sales__c
  ];

  year18 = [Select Contact_Name__c, Contact__c, Account__c, Virtual_Office_Type__c, M__c, Office_No__c,
   Meeting_Room_No__c, Actual_Start_Date_Time__c, End_Date_Time__c, Active__c, X2018_January__c,
   X2018_February__c, X2018_March__c, X2018_April__c, X2018_May__c, X2018_June__c, X2018_July__c,
   X2018_August__c, X2018_September__c, X2018_October__c, X2018_November__c, X2018_December__c
   FROM My_Sales__c
  ];

  year19 = [Select Contact_Name__c, Contact__c, Account__c, Virtual_Office_Type__c, M__c, Office_No__c,
   Meeting_Room_No__c, Actual_Start_Date_Time__c, End_Date_Time__c, Active__c, X2019_January__c,
   X2019_February__c, X2019_March__c, X2019_April__c, X2019_May__c, X2019_June__c, X2019_July__c,
   X2019_August__c, X2019_September__c, X2019_October__c, X2019_November__c, X2019_December__c
   FROM My_Sales__c
  ];

  year20 = [Select Contact_Name__c, Contact__c, Account__c, Virtual_Office_Type__c, M__c, Office_No__c,
   Meeting_Room_No__c, Actual_Start_Date_Time__c, End_Date_Time__c, Active__c, X2020_January__c,
   X2020_February__c, X2020_March__c, X2020_April__c, X2020_May__c, X2020_June__c, X2020_July__c,
   X2020_August__c, X2020_September__c, X2020_October__c, X2020_November__c, X2020_December__c
   FROM My_Sales__c
  ];


  for (My_Sales__c ms: jan15) {
   if (selectedMonth == 1 && year == 2015 && ms.X2015_January__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 2 && year == 2015 && ms.X2015_February__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 3 && year == 2015 && ms.X2015_March__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 4 && year == 2015 && ms.X2015_April__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 5 && year == 2015 && ms.X2015_May__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 6 && year == 2015 && ms.X2015_June__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 7 && year == 2015 && ms.X2015_July__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 8 && year == 2015 && ms.X2015_August__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 9 && year == 2015 && ms.X2015_September__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 10 && year == 2015 && ms.X2015_October__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 11 && year == 2015 && ms.X2015_November__c == true) {
    subBookings.add(ms);
   } else if (selectedMonth == 12 && year == 2015 && ms.X2015_December__c == true) {
    subBookings.add(ms);
   }
  }
  for (My_Sales__c mp: year16) {
   if (selectedMonth == 1 && year == 2016 && mp.X2016_January__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 2 && year == 2016 && mp.X2016_February__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 3 && year == 2016 && mp.X2016_March__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 4 && year == 2016 && mp.X2016_April__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 5 && year == 2016 && mp.X2016_May__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 6 && year == 2016 && mp.X2016_June__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 7 && year == 2016 && mp.X2016_July__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 8 && year == 2016 && mp.X2016_August__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 9 && year == 2016 && mp.X2016_September__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 10 && year == 2016 && mp.X2016_October__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 11 && year == 2016 && mp.X2016_November__c == true) {
    subBookings.add(mp);
   } else if (selectedMonth == 12 && year == 2016 && mp.X2016_December__c == true) {
    subBookings.add(mp);
   }
  }

  for (My_Sales__c ma: year17) {
   if (selectedMonth == 1 && year == 2017 && ma.X2017_January__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 2 && year == 2017 && ma.X2017_February__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 3 && year == 2017 && ma.X2017_March__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 4 && year == 2017 && ma.X2017_April__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 5 && year == 2017 && ma.X2017_May__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 6 && year == 2017 && ma.X2017_June__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 7 && year == 2017 && ma.X2017_July__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 8 && year == 2017 && ma.X2017_August__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 9 && year == 2017 && ma.X2017_September__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 10 && year == 2017 && ma.X2017_October__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 11 && year == 2017 && ma.X2017_November__c == true) {
    subBookings.add(ma);
   } else if (selectedMonth == 12 && year == 2017 && ma.X2017_December__c == true) {
    subBookings.add(ma);
   }
  }

  for (My_Sales__c mo: year18) {
   if (selectedMonth == 1 && year == 2018 && mo.X2018_January__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 2 && year == 2018 && mo.X2018_February__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 3 && year == 2018 && mo.X2018_March__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 4 && year == 2018 && mo.X2018_April__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 5 && year == 2018 && mo.X2018_May__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 6 && year == 2018 && mo.X2018_June__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 7 && year == 2018 && mo.X2018_July__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 8 && year == 2018 && mo.X2018_August__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 9 && year == 2018 && mo.X2018_September__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 10 && year == 2018 && mo.X2018_October__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 11 && year == 2018 && mo.X2018_November__c == true) {
    subBookings.add(mo);
   } else if (selectedMonth == 12 && year == 2018 && mo.X2018_December__c == true) {
    subBookings.add(mo);
   }
  }

  for (My_Sales__c me: year19) {
   if (selectedMonth == 1 && year == 2019 && me.X2019_January__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 2 && year == 2019 && me.X2019_February__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 3 && year == 2019 && me.X2019_March__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 4 && year == 2019 && me.X2019_April__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 5 && year == 2019 && me.X2019_May__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 6 && year == 2019 && me.X2019_June__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 7 && year == 2019 && me.X2019_July__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 8 && year == 2019 && me.X2019_August__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 9 && year == 2019 && me.X2019_September__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 10 && year == 2019 && me.X2019_October__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 11 && year == 2019 && me.X2019_November__c == true) {
    subBookings.add(me);
   } else if (selectedMonth == 12 && year == 2019 && me.X2019_December__c == true) {
    subBookings.add(me);
   }
  }

  for (My_Sales__c mx: year20) {
   if (selectedMonth == 1 && year == 2020 && mx.X2020_January__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 2 && year == 2020 && mx.X2020_February__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 3 && year == 2020 && mx.X2020_March__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 4 && year == 2020 && mx.X2020_April__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 5 && year == 2020 && mx.X2020_May__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 6 && year == 2020 && mx.X2020_June__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 7 && year == 2020 && mx.X2020_July__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 8 && year == 2020 && mx.X2020_August__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 9 && year == 2020 && mx.X2020_September__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 10 && year == 2020 && mx.X2020_October__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 11 && year == 2020 && mx.X2020_November__c == true) {
    subBookings.add(mx);
   } else if (selectedMonth == 12 && year == 2020 && mx.X2020_December__c == true) {
    subBookings.add(mx);
   }
  }

  return null;
 }


}

Can some one please help me with the test class.
Abhilash Mishra 13Abhilash Mishra 13
Hi,
about test class and test methods you can read from the following links
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

now about your code , since it contains alot of if and else, you have to create a test environment(all the possible use cases in with either if or else will be true )  for every condition. only then you will able to cover all the code.

Please upvote if this helps.
Happy coding !!!!!!
Thanks 
Abhilash Mishra
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about test classes.
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
 
@isTest 
public class RoomOccupancyRptCntlTest 
{
	static testMethod void testMethod1() 
	{

		My_Sales__c sal= new My_Sales__c();
		sal=Name='demo';
		// add all required field
		insert sal ;
		
		Test.StartTest();
			RoomOccupancyRptCntl obj = new RoomOccupancyRptCntl();
			obj.additionalFilter();
			
		Test.StopTest();
	}
}

Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


Please let us know if this post will help you

 
Mahesh DMahesh D
Hi,

I am not able to understand your logic and requirement.

Why are you writing the same query multiple times and capturing into

jan15, jan16, jan17, jan18, jan19, jan20.

We can write the code in a better way by following the best practicies and governor limits.

Once we change the logic properly, it will be easy to write the Test Class.

Regards,
Mahesh