function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DRobi83DRobi83 

Need Help Creating Test APEX Class

Hello

 

I am new to Apex and just starting to learn it, so I would appreciate any help here.

 

I was informed the class needs to pass 75% before i can depoly it to my production org

 

Can anyone help me write a Test Class for this? The page is simply querying some related records with a IF filter criteria

 

Here is my page

 

<apex:page showHeader="false" standardController="miiProject__Project__c" extensions="OngoingCostsList,SetupCostsList">
<html>
<head>
  <style> 
     body {font-family: Arial Unicode MS;}
     .companyName {font-weight:bold;font-size:30px;color:red} 
     .projectplan {font-family: Arial, Helvetica, sans-serif;font-size: 36px;} 
     .projectdetails {font-family: Arial, Helvetica, sans-serif;font-size: 18px;} 
     .body {font-family: Arial, Helvetica, sans-serif;font-size: 14px; vertical-align:top; text-align:top; } 
     .headerbackgroundblue {font-family: Arial, Helvetica, sans-serif;font-size: 22px;background-color:#003399;color: #FFFFFF;font-weight:bold;}
     .headerbackgroundgrey {font-family: Arial, Helvetica, sans-serif;font-size: 14px;background-color:#EFEFEF;color: #000000;font-weight:bold;}
     .headerbackgroundorange {font-family: Arial, Helvetica, sans-serif;font-size: 22px;background-color:#FFC993;color: #000000;font-weight:bold;}
  </style>
</head>
<table width="100%" border="0" cellpadding="5">
  <tr>
    <td><div align="center"><img src="http://www.miiworks.com/images/miiworks-177x120.png" width="177" height="120"></img></div></td>
  </tr>
  <tr>
    <td><div align="center" class="projectplan">Project Plan</div></td>
  </tr>
</table>
<br></br>
<table width="20%" border="0" align="center" cellpadding="5">
  <tr>
    <td class="body">Company:</td>
    <td class="body"><strong><apex:outputField value=" {!miiProject__Project__c.miiProject__Account_Name__c}"/></strong></td>
  </tr>
  <tr>
    <td class="body">Document Owner:</td>
    <td class="body"><strong>miiWorks Pty Ltd</strong></td>
  </tr>
  <tr>
    <td class="body">Start Date:</td>
    <td class="body"><strong><apex:outputField value="{!miiProject__Project__c.miiProject__Date_Project_Start__c}"/></strong></td>
  </tr>
  <tr>
    <td class="body">End Date:</td>
    <td class="body"><strong><apex:outputField value="{!miiProject__Project__c.miiProject__Date_Project_End__c}"/></strong></td>
  </tr>
</table>
<br></br>
<table width="20%" border="0" align="center" cellpadding="5">
  <tr>
    <td class="body">Summary:</td>
    <td class="body"><strong><apex:outputField value="{!miiProject__Project__c.miiProject__Subject__c}"/></strong></td>
  </tr>
  <tr>
    <td class="body">Description:</td>
    <td class="body"><apex:outputField value="{!miiProject__Project__c.miiProject__Description__c}"/></td>
  </tr>
</table>

<br></br><br></br>
    
   <apex:outputPanel >
<table border="1" cellpadding="5" width="75%" align="center" valign="top"> 
     <tr>
       <td class="headerbackgroundblue">AREA MANAGERS / RESPONSIBILITIES</td>
     </tr>  
    <apex:dataTable value="{!miiProject__Project__c.Project_Contacts__r}" var="c" cellpadding="5" border="1" width="75%" rows="4" align="center">
       <apex:column headerClass="headerbackgroundgrey" headerValue="Name" value="{!c.Contact__c}" width="100px"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Company" value="{!c.Account_Name__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Role" value="{!c.Role__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Responsibilities" value="{!c.Responsibilities__c}"/>
    </apex:dataTable>
</table> 
&nbsp;

   <table border="1" cellpadding="5" width="75%" align="center">
     <tr>
       <td class="headerbackgroundblue">GUIDELINES</td>
     </tr>
     <tr>
       <td><br></br>
         <strong class="body">1. EXECUTION OF  MILESTONES</strong><br></br><br></br>
         The project execution must be  completed in the explicit order of the milestones to achieve project success.
         &nbsp;<br></br><br></br>
         <strong class="body">2. OVERDUE TASKS</strong><br></br><br></br>
         If a task falls overdue, the person responsible  for the task should make every effort to complete the task in a timely manner  or propose a new completion date to the project team members.
         &nbsp;<br></br><br></br>
         <strong class="body">3. PROJECT FAILURE</strong><br></br><br></br>
         If a milestone has not been reached  by its due date due to one or many overdue tasks, the project will fall into  alert status and a priority team meeting will be called to discuss the  milestone and overdue tasks.
         &nbsp;<br></br><br></br>
         The project manager reserves the  right to re-allocate the task to another team member to complete the task or  freeze the project if there is a lack of commitment and/or resources from the  team member.
         &nbsp;<br></br><br></br>
         <strong class="body">4. LOCALE</strong><br></br><br></br>
         Please take note:

         <p><ul>Date Format: dd/mm/yy (please note that long  date format may be used)</ul>
         <ul>Language of Document: English (British)</ul></p>

           <strong class="body">5. TERMINOLOGY</strong><br></br><br></br>

        UAT – User Acceptance  Testing</td>
     </tr>
   </table>
   <br></br>
   </apex:outputPanel> 
<table border="1" cellpadding="5" width="75%" align="center"> 
     <tr>
       <td class="headerbackgroundblue">MILESTONES</td>
     </tr>  
 <apex:dataTable value="{!miiProject__Project__c.miiProject__Project_Milestones__r}" var="m" cellpadding="5" border="1" width="75%" align="center">
       <apex:column headerClass="headerbackgroundgrey" headerValue="Milestone" value="{!m.miiProject__Subject__c}"/> 
       <apex:column headerClass="headerbackgroundgrey" headerValue="Due Date" value="{!m.miiProject__Date_Due__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Completed Date" value="{!m.miiProject__Date_Time_Completed__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Signed Off?"/>
   </apex:dataTable>
</table>
<br></br>
<table border="1" cellpadding="5" width="75%" align="center"> 
     <tr>
       <td class="headerbackgroundblue">TASKS</td>
     </tr>
 <apex:dataTable value="{!miiProject__Project__c.Project_Tasks__r}" var="t" cellpadding="5" border="1" width="75%" align="center">
       <apex:column headerClass="headerbackgroundgrey" headerValue="Milestone" value="{!t.Milestone_Subject__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Task" value="{!t.miiProject__Subject__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Detail" value="{!t.miiProject__Description__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Assigned To" value="{!t.miiProject__Assigned_To__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Due Date" value="{!t.miiProject__Date_Due__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Status" value="{!t.miiProject__Status__c}"/>
 </apex:dataTable>
</table> 
<br></br>
<table border="1" cellpadding="5" width="75%" align="center"> 
     <tr>
       <td class="headerbackgroundblue">RISK/ENHACEMENT ANALYSIS LOG</td>
     </tr>  
   <apex:dataTable value="{!miiProject__Project__c.Project_Risks_Enhancments__r}" var="ri" cellpadding="5" border="1" width="75%" align="center" >            
       <apex:column headerClass="headerbackgroundgrey" headerValue="ID" value="{!ri.Name}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Nature Of Risk Or Uncertainty" value="{!ri.Subject__c}"/>  
       <apex:column headerClass="headerbackgroundgrey" headerValue="Description Of Requirements" value="{!ri.Description__c}"/>  
       <apex:column headerClass="headerbackgroundgrey" headerValue="Status" value="{!ri.Status__c}"/>
   </apex:dataTable>  
</table> 
<br></br>
<table border="1" cellpadding="5" width="75%" align="center"> 
     <tr>
       <td class="headerbackgroundblue">APPENDICES</td>
     </tr>  
   <apex:dataTable value="{!miiProject__Project__c.Project_Appendixs__r}" var="ap" cellpadding="5" border="1" width="75%" align="center" >            
       <tr>
       <apex:column headerClass="headerbackgroundgrey" headerValue="ID" value="{!ap.Name} - {!ap.Subject__c}"/><br></br>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Description" value="{!ap.Description__c}"/>  
       </tr>
   </apex:dataTable>  
</table>
<br></br>
<table border="1" cellpadding="5" width="75%" align="center"> 
     <tr>
       <td class="headerbackgroundorange">SETUP COSTS</td>
     </tr>  
   <apex:dataTable value="{!SetupCostsList}" var="cose" cellpadding="5" border="1" width="75%" align="center" >            
       <tr>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Description" value="{!cose.Description__c}"/>  
       <apex:column headerClass="headerbackgroundgrey" headerValue="Cost Per Hour" value="{!cose.Amount_Per_Hour__c}"/>  
       <apex:column headerClass="headerbackgroundgrey" headerValue="Hour(s)" value="{!cose.Hours__c}"/> 
       <apex:column headerClass="headerbackgroundgrey" headerValue="Setup Cost" value="{!cose.Cost_Total__c}"/>   
       </tr>
   </apex:dataTable>  
</table> 
<br></br>
<table border="1" cellpadding="5" width="75%" align="center"> 
     <tr>
       <td class="headerbackgroundorange">ONGOING COSTS</td>
     </tr>  
   <apex:dataTable value="{!OngoingCostsList}" var="coon" cellpadding="5" border="1" width="75%" rows="5" align="center">           
       <tr>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Product" value="{!coon.Product_Name__c}"/><br></br>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Description" value="{!coon.Description__c}"/>  
       <apex:column headerClass="headerbackgroundgrey" headerValue="# Users" value="{!coon.Number_Of_Users__c}"/>  
       <apex:column headerClass="headerbackgroundgrey" headerValue="$ Per User" value="{!coon.Amount_Per_User__c}"/>
       <apex:column headerClass="headerbackgroundgrey" headerValue="Total/Month" value="{!coon.Cost_Total__c}"/>  
       </tr>
   </apex:dataTable>  
</table>   
</html> 
</apex:page>

 

Here are my 2 classes

 

public with sharing class OngoingCostsList {
    public OngoingCostsList(ApexPages.StandardController controller) {
    }
    List<Project_Cost__c> myList;
    Public List<Project_Cost__c> getOngoingCostsList() {
        myList = [
        SELECT Id, Name, Type__c, Product_For_Sale__c, Amount_Per_Hour__c, Hours__c, Cost_Total__c, URL__c, Amount_Per_User__c, Description__c, Number_Of_Users__c, Product_Name__c, Cost_Total_Dollars__c FROM Project_Cost__c 
        WHERE Type__c = 'On Going' 
        ORDER BY Name DESC 
        ];
    /// ORDER BY CAN BE ASC OR DESC    
    return  myList;
    }     
}

 

 

public class SetupCostsList {
    public SetupCostsList(ApexPages.StandardController controller) {
    }
    List<Project_Cost__c> myList;
    Public List<Project_Cost__c> getSetupCostsList() {
        myList = [SELECT Id, Name, Type__c, Product_For_Sale__c, Amount_Per_Hour__c, Hours__c, Cost_Total__c, URL__c, Amount_Per_User__c, Description__c, Number_Of_Users__c, Product_Name__c, Cost_Total_Dollars__c FROM Project_Cost__c WHERE Type__c = 'Setup' ORDER BY Name DESC ];
    /// ORDER BY CAN BE ASC OR DESC  
    return  myList;
    }      
}

 

Thank you in advance


David

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code with class and there corresponding test method inside the class:

////////////////// Class 1 ////////////////////////////////////

public with sharing class OngoingCostsList

{

    public OngoingCostsList(ApexPages.StandardController controller)

    {

    }

    List<Project_Cost__c> myList;

    Public List<Project_Cost__c> getOngoingCostsList() {

myList = [

        SELECT Id, Name, Type__c, Product_For_Sale__c, Amount_Per_Hour__c, Hours__c, Cost_Total__c, URL__c, Amount_Per_User__c, Description__c, Number_Of_Users__c, Product_Name__c, Cost_Total_Dollars__c FROM Project_Cost__c

        WHERE Type__c = 'On Going'

        ORDER BY Name DESC

        ];

    /// ORDER BY CAN BE ASC OR DESC   

    return  myList;

    }

    public static testmethod void Test1()

    {

    Project_Cost__c p=new Project_Cost__c();

    ApexPages.StandardController sc = new ApexPages.standardController(p);

           

    OngoingCostsList Ong=new OngoingCostsList(sc);

    Ong.getOngoingCostsList();

    }    

}

/// // Class 2//////////////////////////////////////////

public class SetupCostsList {

    public SetupCostsList(ApexPages.StandardController controller)

    {

    }

    List<Project_Cost__c> myList;

    Public List<Project_Cost__c> getSetupCostsList() {

myList = [SELECT Id, Name, Type__c, Product_For_Sale__c, Amount_Per_Hour__c, Hours__c, Cost_Total__c, URL__c, Amount_Per_User__c, Description__c, Number_Of_Users__c, Product_Name__c, Cost_Total_Dollars__c FROM Project_Cost__c WHERE Type__c = 'Setup' ORDER BY Name DESC ];

    /// ORDER BY CAN BE ASC OR DESC 

    return  myList;

    }

    public static testmethod void Test1()

    {

    Project_Cost__c p=new Project_Cost__c();

    ApexPages.StandardController sc = new ApexPages.standardController(p);

           

    SetupCostsList Ong=new SetupCostsList (sc);

    Ong.getSetupCostsList();

    }     

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code with class and there corresponding test method inside the class:

////////////////// Class 1 ////////////////////////////////////

public with sharing class OngoingCostsList

{

    public OngoingCostsList(ApexPages.StandardController controller)

    {

    }

    List<Project_Cost__c> myList;

    Public List<Project_Cost__c> getOngoingCostsList() {

myList = [

        SELECT Id, Name, Type__c, Product_For_Sale__c, Amount_Per_Hour__c, Hours__c, Cost_Total__c, URL__c, Amount_Per_User__c, Description__c, Number_Of_Users__c, Product_Name__c, Cost_Total_Dollars__c FROM Project_Cost__c

        WHERE Type__c = 'On Going'

        ORDER BY Name DESC

        ];

    /// ORDER BY CAN BE ASC OR DESC   

    return  myList;

    }

    public static testmethod void Test1()

    {

    Project_Cost__c p=new Project_Cost__c();

    ApexPages.StandardController sc = new ApexPages.standardController(p);

           

    OngoingCostsList Ong=new OngoingCostsList(sc);

    Ong.getOngoingCostsList();

    }    

}

/// // Class 2//////////////////////////////////////////

public class SetupCostsList {

    public SetupCostsList(ApexPages.StandardController controller)

    {

    }

    List<Project_Cost__c> myList;

    Public List<Project_Cost__c> getSetupCostsList() {

myList = [SELECT Id, Name, Type__c, Product_For_Sale__c, Amount_Per_Hour__c, Hours__c, Cost_Total__c, URL__c, Amount_Per_User__c, Description__c, Number_Of_Users__c, Product_Name__c, Cost_Total_Dollars__c FROM Project_Cost__c WHERE Type__c = 'Setup' ORDER BY Name DESC ];

    /// ORDER BY CAN BE ASC OR DESC 

    return  myList;

    }

    public static testmethod void Test1()

    {

    Project_Cost__c p=new Project_Cost__c();

    ApexPages.StandardController sc = new ApexPages.standardController(p);

           

    SetupCostsList Ong=new SetupCostsList (sc);

    Ong.getSetupCostsList();

    }     

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
ClintLeeClintLee

David,

 

You're right, you do need to achieve a minimum of 75% code coverage of your Apex code(  but I'd suggest shooting for 100% ).  In addition to code coverage, you'll also want to make the necessary assertions that you are getting the intended results.  There are fortunately a number of resources to help you with understanding Apex testing better.  Here are a few:

 

 

To test Controllers and Extensions you first need to set your VF page and then instantiate your controller and/or extension objects.  Use the controller and extension objects to call their respective methods.  You'll most likely want to create test data in your test method so that your tests are not dependent on any existing data in the database as this can likely lead to problems when you deploy the code to Production. 

 

In your case, it looks like you have a couple of controller extensions.  The following test method might get you going in the right direction.

 

static testmethod void testOne() {

 

           miiProject__Project__c proj = new miiProject__Project__c( Name = 'Test Project', field2 = field2value, ... );

           insert proj;

 

           // You may need to create additional data here, such as Project_Cost__c records related to the Project.

 

           PageReference pageRef = Page.YourPageName;                                             // instantiate you VF page object

           Test.setCurrentPage( pageRef );                                                                             // set the page to the current page for testing

           ApexPages.StandardController stndController                                                    // create a StandardController and pass in your Project object.

                                                     = new ApexPages.StandardController( proj );

           OngoingCostsList ext1 = new OngoingCostsList( stndController );                 // create an object for your first extension, using the standardcontroller in the constructor

           SetupCostsList  ext2     = new SetupCostsList( stndController );                      // create an object for your the second extension

 

           List<Project_Cost__c> ongoingCostsList  = ext1.getOngoingCostsList();           // call the method in the first extension and store the result

           List<Project_Cost__c> setupCostsList       = ext2.getSetupCostsList();                // call the method in the second extension

 

 

           // Now you want to make your assertions like size of the list, elements inside, etc.

           // For example, say you created 3 Project_Cost__c records of each type that should

           // be returned in your get() methods.

 

           System.assertEquals( 3, ongoingCostsList.size() );

           System.assertEquals( 3, setupCostsList.size() );

}

 

 

Hope that helps!

 

~ Clint

DRobi83DRobi83

Thank you everyone for great community support. The reason Salesforce is number 1!