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
James Roberts 32James Roberts 32 

Test class for Apex Controller for Visualforce page

I have build the following Visual Force page for a custom object (Opportunity_Pricing__c) and Standard Controller and I am trying to add a test for Code coverage, but I only get 45% coverage

Here is the visualforce page:
<apex:page standardController="Opportunity_Pricing__c" extensions="PricingTable" lightningStylesheets="true">
    <apex:slds />
    <apex:form >
       <apex:pageBlock >
           <apex:pageBlockButtons >
               <apex:commandButton value="Save" action="{!save}" oncomplete="ReloadPage();" />
        </apex:pageBlockButtons>
           <table>
               <tr >
                   <th></th>
                   <th>Included</th>
                   <th>Year 1</th>
               </tr>                    
               <apex:repeat value="{!OPList}" var="a" >
                   <tr>
                       <th>Decisioning Cloud</th>
                       <td><apex:inputField value="{! a.Decisioning_Cloud_Included__c}"/></td>
                       <td><apex:inputField value="{! a.Decisioning_Cloud_Fee_Year_1__c}"/></td>
                   </tr>
                   <tr>
                       <th>Data Cloud</th>
                       <td><apex:inputField value="{! a.Data_Cloud_Included__c}"/></td>
                       <td><apex:inputField value="{! a.Data_Cloud_Fee_Year_1__c}"/></td>
                   </tr>
                    <tr>
                       <th>Solutions Cloud</th>
                       <td><apex:inputField value="{! a.Solutions_Cloud_Included__c}"/></td>
                       <td><apex:inputField value="{! a.Solutions_Cloud_Fee_Year_1__c}"/></td>
                   </tr>
                   <tr>
                       <th>Insight Cloud</th>
                       <td><apex:inputField value="{! a.Insight_Cloud_Included__c}"/></td>
                       <td><apex:inputField value="{! a.Insight_Cloud_Fee_Year_1__c}"/></td>
                   </tr>
                   <tr>
                       <th>Total Monthly Fees</th>
                       <td></td>
                       <td><apex:inputField value="{! a.Total_Fees_Year_1__c}"/></td>
                   </tr>
            </apex:repeat>
        </table>
       </apex:pageBlock>
   </apex:form>
   <script>
    function ReloadPage(){
           //alert('reload');
           var urlId = '{!$CurrentPage.parameters.Id}';
           //alert(urlId);
           window.top.location='/'+urlId;
           return false;
       }
   </script>
</apex:page>

Here is the Extension:
public class PricingTable{
 public String LId {get;set;}
 public List<Opportunity_Pricing__c> OPList {get;set;}
 public PricingTable(ApexPages.StandardController controller){
       LId = '';
       LId=System.currentPageReference().getParameters().get('id');
       System.debug('...................LID=='+LId);
       Id i= Id.valueOf(LId);
       OPList = [Select Base_Subscription_Fee_Included__c,Base_Subscription_Fee_Year_1__c,Base_Subscription_Fee_Year_2__c,Base_Subscription_Fee_Year_3__c,Base_Subscription_Fee_Year_4__c,Base_Subscription_Fee_Year_5__c,
                         Enhanced_Support_Included__c,Enhanced_Support_Fee_Year_1__c,Enhanced_Support_Fee_Year_2__c,Enhanced_Support_Fee_Year_3__c,Enhanced_Support_Fee_Year_4__c,Enhanced_Support_Fee_Year_5__c,
                         High_Availability_Included__c,High_Availability_Fee_Year_1__c,High_Availability_Fee_Year_2__c,High_Availability_Fee_Year_3__c,High_Availability_Fee_Year_4__c,High_Availability_Fee_Year_5__c,
                         Additional_Data_Connections_Included__c, Add_Data_Cons_Fees_Y1__c, Add_Data_Cons_Fees_Y2__c, Add_Data_Cons_Fees_Y3__c, Add_Data_Cons_Fees_Y4__c, Add_Data_Cons_Fees_Y5__c,
                         Adaptors_Included__c, Adaptor_Fees_Year_1__c, Adaptor_Fees_Year_2__c, Adaptor_Fees_Year_3__c,Adaptor_Fees_Year_4__c,Adaptor_Fees_Year_5__c,
                         Model_Integrations_Included__c, Model_Integration_Fees_Year_1__c, Model_Integration_Fees_Year_2__c,    Model_Integration_Fees_Year_3__c,Model_Integration_Fees_Year_4__c,Model_Integration_Fees_Year_5__c,
                         Risk_Repository_Included__c,Risk_Repository_Fees_Year_1__c,Risk_Repository_Fees_Year_2__c,Risk_Repository_Fees_Year_3__c,Risk_Repository_Fees_Year_4__c,Risk_Repository_Fees_Year_5__c,
                         SAS_Model_Included__c, SAS_Model_Fees_Year_1__c, SAS_Model_Fees_Year_2__c,SAS_Model_Fees_Year_3__c,SAS_Model_Fees_Year_4__c,SAS_Model_Fees_Year_5__c,
                         Virtual_Private_Cloud_Included__c,Virtual_Private_Cloud_Fees_Year_1__c,Virtual_Private_Cloud_Fees_Year_2__c,Virtual_Private_Cloud_Fees_Year_3__c,Virtual_Private_Cloud_Fees_Year_4__c,Virtual_Private_Cloud_Fees_Year_5__c,
                         Business_Critical_Support_Included__c,Business_Critical_Support_Fees_Year_1__c,Business_Critical_Support_Fees_Year_2__c,Business_Critical_Support_Fees_Year_3__c,Business_Critical_Support_Fees_Year_4__c,Business_Critical_Support_Fees_Year_5__c,
                         PCI_Compliance_Included__c,PCI_Compliance_Fees_Year_1__c,PCI_Compliance_Fees_Year_2__c,PCI_Compliance_Fees_Year_3__c,PCI_Compliance_Fees_Year_4__c,PCI_Compliance_Fees_Year_5__c,
                         Value_Added_Services_Included__c,Value_Added_Services_Fees_Year_1__c,Value_Added_Services_Fees_Year_2__c,Value_Added_Services_Fees_Year_3__c,Value_Added_Services_Fees_Year_4__c,Value_Added_Services_Fees_Year_5__c,
                         Total_Fees_Year_1__c,Total_Fees_Year_2__c,Total_Fees_Year_3__c,Total_Fees_Year_4__c,Total_Fees_Year_5__c,Opportunity_Product_Level__c,Pricing_Sheet_Approved__c,Average_MRR__c,Approved_MRR__c,
                         Data_Cloud_Included__c,Data_Cloud_Fee_Year_1__c,Data_Cloud_Fee_Year_2__c,Data_Cloud_Fee_Year_3__c,Data_Cloud_Fee_Year_4__c,Data_Cloud_Fee_Year_5__c,
                         Insight_Cloud_Included__c,Insight_Cloud_Fee_Year_1__c,Insight_Cloud_Fee_Year_2__c,Insight_Cloud_Fee_Year_3__c,Insight_Cloud_Fee_Year_4__c,Insight_Cloud_Fee_Year_5__c,
                         Solutions_Cloud_Included__c,Solutions_Cloud_Fee_Year_1__c,Solutions_Cloud_Fee_Year_2__c,Solutions_Cloud_Fee_Year_3__c,Solutions_Cloud_Fee_Year_4__c,Solutions_Cloud_Fee_Year_5__c,
                         Decisioning_Cloud_Included__c,Decisioning_Cloud_Fee_Year_1__c,Decisioning_Cloud_Fee_Year_2__c,Decisioning_Cloud_Fee_Year_3__c,Decisioning_Cloud_Fee_Year_4__c,Decisioning_Cloud_Fee_Year_5__c 
                         from Opportunity_Pricing__c where Id=:i];
       System.debug('.............................OPlist=='+OPList);
   }
 public PricingTable(){
   }
 public void save(){
       update OPList;
       /*PageReference pr = new PageReference('/'+OPList[0].Id);
       pr.setRedirect(true);
       return pr;*/
        }
   }

Here is the addition to the test class:
        ApexPages.StandardController sc = new ApexPages.StandardController(oppprice);
        PricingTable testPT = new PricingTable(sc);
        Test.StopTest();
Suraj Tripathi 47Suraj Tripathi 47
Hi James robert,

You can take reference from this below code.
public class PricingTable_Test {
    @istest static void method(){
        try{
            Opportunity_Pricing__c oppprice= new Opportunity_Pricing__c();
            oppprice.Base_Subscription_Fee_Included__c='xyz';
            oppprice.Base_Subscription_Fee_Year_1__c='abc';
            //if you want to add the field then add...
            insert oppprice;
            test.startTest();
            ApexPages.currentPage().getParameters().put('id',oppprice.Id);
            ApexPages.StandardController sc = new ApexPages.StandardController(oppprice);
            PricingTable testPT = new PricingTable(sc);
            testPT.save();
            Test.StopTest();
        }
        catch(Exception e){
            system.debug(e.getMessage()+'    '+e.getLineNumber());
        }
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.