+ Start a Discussion
Alex Waddell 18Alex Waddell 18 

Help writing test class for VF Page

Hello, I am trying to write a test class for the following VF Page
I wrote a test class but I am getting 0% code coverage for the controller I am trying to test...
VF Page:
<apex:page Controller="ResourceListController" renderAs="PDF" >
    <apex:form >
            <apex:image id="AdobeLogo" value="{!'/servlet/servlet.FileDownload?file= 0152f0000009UPQ'}" width="100%" height="125"/>
        <br></br>
    <br></br>
<br></br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        <br></br>
    <br></br>
<br></br> 
Please contact Adobe Care and Wellness at 4807205699 for any questions regarding your Resource List.
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>
<div style="text-align:center">
            <apex:image id="ArizonaLandScape" value="{!'/servlet/servlet.FileDownload?file= 0152f0000009Uhy'}" width="75%" height="75%"/>
           </div>

<div style="page-break-after:always;">
</div>
<br></br>
<br></br>
<br></br>

    <apex:repeat var="RL" value="{!ResourceList}"> 
        <table border="1" style="width:100%" >
            <tr>
            <th >Name</th>
                </tr>
            <tr>
          <td>{!RL.Account__r.Name}</td>
                </tr>
                       <tr>
            <th>Description</th>
                </tr>
            <tr>
          <td>{!RL.Account__r.Description}</td>
                </tr>
           <tr>
            <th>Address</th>
           </tr>
            <tr>
          <td>{!RL.Account__r.BillingStreet}<br></br> 
            {!RL.Account__r.BillingCity}, {!RL.Account__r.BillingState} {!RL.Account__r.BillingPostalCode} </td>
                </tr>

            
        </table>
        <table border="1" style="Width:100%">
         <tr>
            <th>Phone</th><th>Type</th><th>Distance</th>
           </tr>
        
                    <tr>
          <td>{!RL.Account__r.Phone}</td><td>{!RL.Type__c}</td><td>{!RL.Distance__c} miles</td>
                </tr>
            </table>
<br></br>
<br></br>
<br></br>
<br></br>
<br></br>




     </Apex:Repeat>
                 
        </apex:form>
</apex:page>
Controller
public class ResourceListController {
   public Id RecordId {get;set;}    
public List<Resource_List__c> getResourceList() {
    RecordId  = ApexPages.currentPage().getParameters().get('id');
    List<Resource_List__c> results = Database.query(
        'SELECT Id, Account__c, Account__r.Name, Distance__c, type__c, Account__r.Phone, Account__r.Description, Account__r.billingStreet, Account__r.billingCity, Account__r.billingPostalCode, Account__r.billingState ' +
        'FROM Resource_List__c ' + 
        'Where Social_Services_Assessment__c = :RecordId' +
        'ORDER BY Type__c asc, Distance__c asc'
    );
    return results;
}
    
    
}
Test Class
@isTest
public class resourceListTestClass {
	static testMethod void testMethod1()
    {
        Account ta = new Account();
        ta.name = 'Alex Demo';
        ta.First_Name__c = 'Alex';
        ta.Surname__c = 'Demo';
        ta.Primary_Insurance__c = 'Test Insurance Company';
        ta.Gender__c ='Male';
        ta.County__c ='Yavapai';
        ta.BillingStreet = '123 West Gurley Street';
        ta.BillingCity = 'Prescott';
        ta.BillingState = 'Arizona';
        ta.BillingPostalCode = '86303';
        ta.RecordTypeId = '012360000004FEPAA2';
        insert ta;
        
        Social_Services_Assesment__c sw = new Social_Services_Assesment__c ();
        sw.Account__c = ta.Id;
        sw.Radius_in_miles__c = 25;
        sw.Monthly_income__c = 5000;
        sw.Do_you_have_enough_money_each_month__c = 'No';
        sw.Do_you_recieve_SSI__c = 'No';
        sw.Do_you_receive_SSDI__c = 'No';
        sw.Do_you_receive_food_assistance__c = 'No';
        sw.Do_you_receive_cash_assistance__c = 'No';
        sw.Do_you_have_adequate_shelter__c = 'No';
        sw.Do_you_need_congregate_meals__c = 'Yes';
        sw.Do_you_need_transportation__c = 'Yes';
        sw.Do_you_have_enough_money_each_month__c = 'No';
        sw.Can_you_pay_for_your_housing__c = 'No';
        sw.Can_you_pay_your_utilities__c = 'No';
        sw.Can_you_pay_for_transportation__c = 'No';
        sw.Can_you_pay_for_medical_visits__c = 'No';
        sw.Are_you_unemployed__c = 'Yes';
        sw.SSI_SSDI_decreased__c = 'Yes';
        sw.RecordTypeId = '0121R000000yZUyQAM';
        insert sw;
        
        Account r1 = new Account ();
        r1.RecordTypeId = '0121R000000yZGLQA2';
        r1.Name = 'Resource 1';
        r1.Food_Bank__c = true;
        r1.Financial_Assistance__c = true;
        insert r1;
        
        Account r2 = new Account ();
        r2.RecordTypeId = '0121R000000yZGLQA2';
        r2.Name = 'Resource 2';
        r2.Food_Bank__c = true;
        r2.Financial_Assistance__c = true;
        insert r2;
        
        Resource_List__c RL1 = new Resource_List__c ();
        RL1.Account__c = r1.Id;
        RL1.Social_Services_Assessment__c = sw.id;
        insert RL1;
        
        Resource_List__c RL2 = new Resource_List__c ();
        RL2.Account__c = r2.Id;
        RL2.Social_Services_Assessment__c = sw.id;
        insert RL2;
        
        Test.StartTest();
		PageReference PageRef = Page.ResourceList;
        Test.setCurrentpage(PageRef);
        System.currentPageReference().getParameters().put('RecordId',sw.Id);
        ResourceListController controller = new ResourceListController();
		Test.StopTest();
        
    }
}

 
Best Answer chosen by Alex Waddell 18
Sachin HoodaSachin Hooda
Hi Alex,
You created an instance of your class but haven't made a call to any method. So, since it would cover the constructor only.  In this scenario, the code coverage would be the 1st 2 to 3 line.
So, you need to call a method of your class from the test class. In between the start & stop test. Please update your code to
test.StartTest();
PageReference PageRef = Page.ResourceList;
test.setCurrentpage(PageRef);
System.currentPageReference().getParameters().put('RecordId',sw.Id);
ResourceListController obj = new ResourceListController();
obj.getResourceList();
test.StopTest();
This will invoke your class method. Which will proceed with the further task. In Case you've any other doubt please post them here.

All Answers

Sachin HoodaSachin Hooda
Hi Alex,
You created an instance of your class but haven't made a call to any method. So, since it would cover the constructor only.  In this scenario, the code coverage would be the 1st 2 to 3 line.
So, you need to call a method of your class from the test class. In between the start & stop test. Please update your code to
test.StartTest();
PageReference PageRef = Page.ResourceList;
test.setCurrentpage(PageRef);
System.currentPageReference().getParameters().put('RecordId',sw.Id);
ResourceListController obj = new ResourceListController();
obj.getResourceList();
test.StopTest();
This will invoke your class method. Which will proceed with the further task. In Case you've any other doubt please post them here.
This was selected as the best answer
Alex Waddell 18Alex Waddell 18
Hello Sachin,

Your solution did work but only when i removed the "Order by" clause in my SOQL query

So when I just put in your code (without changing SOQL query in my class) into the Test class i get the following error;
System.QueryException: unexpected token: 'BY'

Class.ResourceListController.getResourceList: line 5, column 1
Class.resourceListTestClass.testMethod1: line 70, column 1

When i remove the Order By, the test class passes 100%!!!! I need the ordering, any ideas on how to overcome this?