deep chouhan
deep chouhan

Reputation: 1

Testing custom controller for visualforce page

I need help for my contoller test class.I have created an inbound change set in my Production, and an outbound in my Sandbox. Now I am at the step of going from 22% code coverage to at least a 75% code coverage, and not having done this before. This is my VisualForce Page.

<apex:page controller="MyDealsheetController" tabStyle="Dealsheet__c">
  <apex:form >
      <apex:pageMessages />
      <apex:pageBlock title="Create DealSheet">
      <apex:pageBlockButtons >
      <apex:commandButton action="{! Addrecord}" value="Create Dealsheet"/>
      </apex:pageBlockButtons>

<apex:pageBlockSection columns="2" title="Dealsheet Detail">
<apex:inputField value="{! Dealsheet.Trade_Date__c }"/>
<apex:inputField value="{! Dealsheet.Buy_Sell__c }"/> 
<apex:selectList size="1" value="{! Dealsheet.Counter_Party__c}" multiselect="false">
<apex:selectOptions value="{!CPList}"></apex:selectOptions>
</apex:selectList>
<apex:selectList size="1" value="{! Dealsheet.Pipe_Line__c}" multiselect="false">
<apex:selectOptions value="{!PipelineList}"></apex:selectOptions>
</apex:selectList>  
<apex:inputField value="{! Dealsheet.Start_Date__c }"/>
<apex:inputField value="{! Dealsheet.End_Date__c }"/>
<apex:inputField value="{! Dealsheet.Broker__c }"/>
<apex:inputField value="{! Dealsheet.Brokerage_Per_MMBTU__c }"/>
<apex:inputField value="{! Dealsheet.CP_Trader__c }"/>
<apex:inputField value="{! Dealsheet.CP_Confirm_Email__c }"/>
<apex:inputField value="{! Dealsheet.Deal_Type__c }"/>
<apex:inputField value="{! Dealsheet.Delivery_Point__c }"/>
<apex:inputField value="{! Dealsheet.Volume_MMBTU_Per_Day__c }"/>
<apex:inputField value="{! Dealsheet.PriceIndex_Name__c }"/>
<apex:inputField value="{! Dealsheet.Tradebook__c }"/>
</apex:pageBlockSection>         
<apex:PageblockSection columns="1" >
 <apex:PageBlockSectionItem >
 <apex:outputLabel value="Price Type"/>
   <apex:actionRegion >
        <apex:inputField label="Price Type" value="{!Dealsheet.Price_Type__c}">
          <apex:actionSupport event="onchange" reRender="ajaxrequest" />
           </apex:inputField>
         </apex:actionRegion>
        </apex:PageBlockSectionItem>
           </apex:PageblockSection>
         <apex:outputPanel id="ajaxrequest">  
          <apex:pageBlockSection rendered="{!Dealsheet.Price_Type__c =='Fixed'}" >
           <apex:inputField value="{!Dealsheet.Fixed_Price__c}"/>          
         </apex:pageBlockSection>
     <apex:pageBlockSection rendered="{!Dealsheet.Price_Type__c =='Floating'}" >
       <apex:inputField value="{! Dealsheet.Price_Diff__c}"  />
     </apex:pageBlockSection>

   <apex:pageBlockSection columns="1" title="Comments">
   <apex:inputField value="{! Dealsheet.Trader_Comments__c }">
   </apex:inputField>
   </apex:pageBlockSection>
   </apex:outputPanel>
  </apex:pageBlock>
 </apex:form>
</apex:page>

This is VisualForce Controller Code -

 public class MyDealsheetController {
 public Dealsheet__c Dealsheet;
 public MyDealsheetController()
 {
    Dealsheet= new Dealsheet__c();
 }
 public Dealsheet__c getDealsheet() {
    return Dealsheet;
 }     
 public List<selectOption> getCPList() {
 List<selectOption>  options= new List<selectOption>(); 
 for (CP__c cp :[SELECT Id, Name FROM CP__c])    
 { 
 options.add(new selectOption(cp.Id, cp.Name)); 
 }
return options; 
}   
public List<selectOption> getPipelineList() {
List<selectOption>  options1= new List<selectOption>(); 
for (NGPIPES__c pipe :[SELECT Id, Name FROM NGPIPES__c])    
{ 
options1.add(new selectOption(pipe.Id, pipe.Name)); 
}
return options1; 
}   
public List<selectOption> getCTList() {
List<selectOption> options2=new List<selectOption>(); 
for (CP_Trader__c CT :[SELECT Id, Name FROM CP_Trader__c])    
{ 
options2.add(new selectOption(CT.Id, CT.Name)); 
}
return options2; 
}  
public List<selectOption> getDPList() {
List<selectOption> options3= new List<selectOption>(); 
for (Delivery_Point__c DP :[SELECT Id, Name FROM Delivery_Point__c])    
{ 
options3.add(new selectOption(DP.Id, DP.Name)); 
}
return options3; 
} 
public List<selectOption> getBrokerList() {
List<selectOption> options4= new List<selectOption>(); 
for (CP_Broker__c Br :[SELECT Id, Name FROM CP_Broker__c])    
{ 
 options4.add(new selectOption(Br.Id, Br.Name)); 
}
 return options4; 
 }  
public List<selectOption> getPIList() {
List<selectOption> options5=new List<selectOption>(); 
for (PRICEINDEX__c PI :[SELECT Id, Name FROM PRICEINDEX__c])    
{ 
 options5.add(new selectOption(PI.Id, PI.Name)); 
}
return options5; 
}  
public PageReference Cancel()
{
 PageReference page = new PageReference('/apex/DealsheetController');
 page.setRedirect(true);
 return page;
}
public PageReference Addrecord()
{
 try {
    insert(Dealsheet);
    PageReference pageRef = ApexPages.currentPage();
    pageRef.setRedirect(true);
    return pageRef;
 }
 Catch(System.DmlException e)
 {

  ApexPages.addMessages(e);
    return null;
  }
 }   
}

and this Test code -

  @isTest
  public class TestMyDealsheetController {
  static testMethod void VerifyTestMyDealsheetController()
  {
  Dealsheet__c ds = new Dealsheet__c ();
            ds.Trade_Date__c=date.parse('01/01/2015');
            ds.Buy_Sell__c='Buy';
            ds.Counter_Party__c=''
            ds.Start_Date__c=date.parse('02/02/2015');
            ds.End_Date__c=date.parse('12/12/2015');
            ds.Volume_MMBTU_Per_Day__c=11;
        test.startTest(); 
        insert(ds);
        test.stopTest();
 ApexPages.currentPage().getParameters().put('DealsheetController','?');
 ApexPages.StandardController stdDS = new   ApexPages.StandardController(ds);
 MyDealsheetController MyDSController = new MyDealsheetController();
        MyDSController.Addrecord();
        MyDSController.Cancel();

 }
}

Please do the needfull please advice.. Regards, Deep

Upvotes: 0

Views: 3537

Answers (1)

RD3
RD3

Reputation: 1089

In order to achieve 75% test coverage, your test class needs to call at least 75% of the lines of your controller class code.

To do this properly, you will need to create multiple test methods. It is a best practice to utilize assert statements to ensure that the return values are good and valid, but it is not a requirement to reach the 75%.

In your current code, you only make three calls to your controller class. You instantiate a new controller, then call the Addrecord, and then call Cancel.

You have seven other methods that never get called. You need to call them to get the correct test coverage.

Because your code is lacking proper formatting, comments, meaningful variable names (why is there an options1, option2, options3, options4, options5 when they are each in different methods?), or context about what is going on, I can only give you a quick and dirty solution (see below). However, the real honest answer is you need to read up on the Salesforce Documentation, take some of their online trainings, and read-up on coding standards in general.

Here is the quick and dirty solution. Add the following below MyDSController.Cancel();

Dealsheet__c dealsheet_return = MyDSController.getDealsheet();
List<selectionOption> getCPList_return = MyDSController.getPGList();
List<selectionOption> getCPList_return = MyDSController.getPipelineList();
List<selectionOption> getCPList_return = MyDSController.getCTList();
List<selectionOption> getCPList_return = MyDSController.getDPList();
List<selectionOption> getCPList_return = MyDSController.getBrokerList();
List<selectionOption> getCPList_return = MyDSController.getPIList();

Upvotes: 1

Related Questions