2

I have written a Custom Controller and VF page that is launched by a custom button. The logic updates a custom object (Resource__c). The navigation and updates work fine. But I can't get my Test class to work. The Test class runs successfully, but the Overall Code Coverage (in the Developer Console) for the Controller class remains 0. I'm trying to add these components to a package and it requires the Test. I've researched this quite a bit and I don't understand how the Test Class/method is associated to the Controller Class.

Any help will be greatly appreciated.

Custom Button:

/apex/Test_NewResource_Page?retURL={!PT_PIPROJ__Project__c.Id}&Project_ID={!PT_PIPROJ__Project__c.Id}

Controller:

public with sharing class MyTestResourceController {

Resource__c record {get;set;}

public MyTestResourceController(ApexPages.StandardController stdController) {   

    record = (Resource__c) stdController.getRecord();
    record.Project__c = ApexPages.currentPage().getParameters().get('Project_ID');
}
}

VF Page:

<apex:page standardController="Resource__c" extensions="MyTestResourceController" showHeader="true"> 

  <p><apex:image url="/servlet/servlet.ImageServer?id=015i0000009iMCK&oid=00Di0000000bSEq&lastMod=1459384439000" width="31" height="31"/><font size="5"><font face="Arial">New Resource</font></font></p>

       <body>
        <div align="Left">

        </div> 
</body>  
  <apex:form >
    <apex:pageBlock title="Resource Edit" mode="edit">
  <apex:pageBlockButtons >
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!cancel}" value="Cancel"/>
  </apex:pageBlockButtons>
  <apex:pageBlockSection title="Information" columns="2">    
          <apex:outputField value="{!Resource__c.Project__c}" /> 
  <apex:inputField value="{!Resource__c.Resource_Type__c}" label="Resource Type" required="true"/>
        <apex:inputField value="{!Resource__c.Resource__c}" label="Resource" required="true"/>
  <apex:inputField value="{!Resource__c.Update_Project_Insight__c}"/>
  <apex:outputField value="{!Resource__c.PI_ProjectID__c}"/>


    </apex:pageBlockSection>
</apex:pageBlock>
  </apex:form>
</apex:page>

Test Class:

@isTest

public class MyTestResourceControllerTests{

public static testmethod void MyResourceController_test01(){
String ProjAcct='001i000000IoBuH';
Datetime ProjSchedStart=Datetime.newInstance(1960, 2, 17);

Project__c project = new Project__c(PT_PIPROJ__Account__c=ProjAcct,PT_PIPROJ__Schedule_Start_Date__c=ProjSchedStart);    

insert project;
ApexPages.currentPage().getParameters().put('Project_ID', project.Id);  


String ResType = 'Project Manager';
String ResID = '003i000000Ds1Oo';
Boolean UPDPI = False;
String PIProjID = 'PI_ID: ABCDEFG';

Resource__c record = new Resource__c(Project__c=project.Id,   Resource_Type__c=ResType, Resource__c=ResID, Update_Project_Insight__c=UPDPI,   PI_ProjectID__c=PIProjID);

Insert Record;


   // Retrieve the new Resource
 record = [SELECT Resource__c FROM Resource__c WHERE Id =:Record.Id];
   System.debug('Resource Inserted: ' + Record.Resource__c);

   // Test that the Resource correctly updated.
  System.assertEquals('003i000000Ds1Oo', record.Resource__c);

} }

7

1 Answer 1

1

You have to actually create the data ie the Project and the Resource object. Check out https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_data and Google. You shouldn't be hardcoding those ids

Project__c project = new Project__c();    
insert project;

ApexPages.currentPage().getParameters().put('Project_ID', project.Id);    

MyTestResourceController controller = new MyTestResourceController();

Then you can do stuff like

controller.AMethodInYourClass();

This is super rough but I think you will get the idea

4
  • 1
    the controller is a standard controller extension so the constructor has a different signature than your example - and is on Resource__c, not Project__c
    – cropredy
    Mar 30, 2017 at 5:30
  • I've modified my test class as follows. It runs clean, but I still don't get coverage on my controller. 'Overall Code Coverage' = 0%. I get an error when I try to use: MyTestResourceController = new MyTestResourceController()
    – Boots
    Mar 31, 2017 at 22:25
  • I've modified my test class as follows. It runs clean, but I still don't get coverage on my controller. 'Overall Code Coverage' = 0%. I get an error when I try to use: MyTestResourceController controller = new MyTestResourceController(); Compile Error: Invalid constructor syntax, name=value pairs can only be used for SObjects.
    – Boots
    Mar 31, 2017 at 22:34
  • @isTest public class MyTestResourceControllerTests{ public static testmethod void MyResourceController_test01(){ String ProjAcct='001i000000IoBuH'; Project__c project = new Project__c(PT_PIPROJ__Account__c=ProjAcct; insert project; ApexPages.currentPage().getParameters().put('Project_ID', project.Id); String ResType = 'Project Manager'; String ResID = '003i000000Ds1Oo'; Resource__c record = new Resource__c (Project__c=project.Id); Insert Record;} }
    – Boots
    Mar 31, 2017 at 22:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .