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
Isaac GomolkaIsaac Gomolka 

Help with test class PLEASE!

Ok so the backstory is:  I had to create a button that when pressed loads a visualforce page with questions on it. After the user answers the questions and clicks the submit button it escalates the case in which he is trying to escalate. I had to make dropbox menus (selectlists) with Yes/No as answers but had to store the answer in a field that is a checkbox.(yes being checked, no being unchecked). Im trying to now make a test class for the extension I needed to make in order for the selectlists to work, but im having trouble. 

I have been trying to make a test class for days and can't figure it out. I've done research and read up on it, but can't make one good enough.  I eventually was able to make one that runs an gets the code coverage, but my boss said "The test should not only provide coverage, but should really verify something. I’d recommend assertions validating that setting the picklist values sets the checkboxes." Ive been trying to make another one for about a week, so it's really frustrating me. PLEASE, can someone make a valid test class that would verify the visualforce page. I'd really appreciate it, I'm struggling.

My boss also explained it by saying "In the unit tests, you can actually code up instantiating a page and executing page button actions or controller methods. You could set the current page to the escalation page, set the values, and then execute the controller methods and see what values it gives you." Thanks again in advanced for the help.

Controller Extension:
public with sharing class EscalationPageExtension { 
    
    public String picklist1{get;set;}
    public String picklist2{get;set;}
    public String picklist3{get;set;}
   
    public EscalationPageExtension(ApexPages.StandardController controller){
        
    }
    
	public List<SelectOption> options { get; private set; }
    {
    options = new List<SelectOption>();
    options.add(new SelectOption('', '-Select-')); // ==> option disabled
    options.add(new SelectOption('false', 'No'));
    options.add(new SelectOption('true', 'Yes'));
    }
    
}

Test Class that doesnt verify enough.
@istest public class EscalationPageExtensionTest {

    public static testMethod void testEscalationPageExtension(){

        Apexpages.StandardController stdController = new Apexpages.StandardController(new Case());
        EscalationPageExtension controller = new EscalationPageExtension(stdController);
        
        controller.picklist1 = 'true';
        controller.picklist2 = 'true';
        controller.picklist3 = 'true';  
        
    }
}

Visualforce Page:
<apex:page standardController="Case" extensions="EscalationPageExtension">
    <apex:pageBlock title="{!$User.FirstName}, Answer Questions to Escalate Case.">
    
    <apex:form id="formID">
      
      <h1>How many users are affected?</h1>
      <apex:inputField value="{! Case.Escalation_Users_Affected__c }" required="true"/>
    
      <br/>
      <h1>Does this affect data integrity?</h1>
      <apex:actionRegion >
          <apex:outputPanel styleClass="requiredInput" layout="block">
          <apex:outputPanel styleClass="requiredBlock" layout="block"/>
          <apex:selectList value="{!picklist1}" size="1" required="true">
          <apex:actionSupport event="onchange" rerender="hidepanel1"/>
          <apex:selectOptions value="{!options}" />
          </apex:selectList>
          </apex:outputPanel>
      </apex:actionRegion>
      <br/>
      <apex:outputPanel id="hidepanel1">
      <apex:outputText value="How?" style="display:{!if(picklist1=='true', 'block', 'none')}"/>
      <apex:inputField value="{!Case.Escalation_Data_Integrity_Explain__c}" required="{!picklist1}" style="display:{!if(picklist1=='true', 'block; width:250px; height:75px;', 'none')}"/>
      <apex:inputCheckbox value="{!Case.Escalation_Data_Integrity__c}" selected="true" style="display:none;" rendered="{!picklist1}"/>
      </apex:outputPanel>
      
      <br/> <br/>
      <h1>Do you have a viable workaround?</h1>
      <apex:actionRegion >
          <apex:outputPanel styleClass="requiredInput" layout="block">
          <apex:outputPanel styleClass="requiredBlock" layout="block"/>
          <apex:selectList value="{!picklist2}" size="1" required="true">
          <apex:actionSupport event="onchange" rerender="hidepanel2"/>
          <apex:selectOptions value="{!options}" />
          </apex:selectList>
          </apex:outputPanel>
      </apex:actionRegion>
      <br/>
      <apex:outputPanel id="hidepanel2">
      <apex:outputText value="What is the viable workround?" style="display:{!if(picklist2=='true', 'block','none')}"/>
      <apex:inputField value="{!Case.Escalation_Viable_Workaround_Explain__c}" required="{!picklist2}" style="display:{!if(picklist2=='true', 'block; width:250px; height:75px;', 'none')}" />
      <apex:inputCheckbox value="{!Case.Escalation_Viable_Workaround__c}" selected="true" style="display:none;" rendered="{!picklist2}"/>
      </apex:outputPanel>
    
      <br/> <br/>
      <h1>Does this affect critical application functionality?</h1>
      <apex:actionRegion >
          <apex:outputPanel styleClass="requiredInput" layout="block">
          <apex:outputPanel styleClass="requiredBlock" layout="block"/>
          <apex:selectList value="{!picklist3}" size="1" required="true">
          <apex:actionSupport event="onchange" rerender="hidepanel3"/>
          <apex:selectOptions value="{!options}" />
          </apex:selectList>
          </apex:outputPanel>
      </apex:actionRegion>
      <br/>
      <apex:outputPanel id="hidepanel3">
      <apex:outputText value="How?" style="display:{!if(picklist3=='true', 'block','none')}"/>
      <apex:inputField value="{!Case.Escalation_App_Functionality_Explain__c}" required="{!picklist3}" style="display:{!if(picklist3=='true', 'block; width:250px; height:75px;', 'none')}" />
      <apex:inputCheckbox value="{!Case.Escalation_Application_Functionality__c}" selected="true" style="display:none;" rendered="{!picklist3}"/>
      </apex:outputPanel>
      
      <br/> <br/>
      <h1>What business functionality are you unable to perform?</h1>
      <br/>
      <apex:inputField value="{! Case.Escalation_Business_Functionality__c }"  style="width: 300px; height: 100px" required="true"/>
      
      <br/> <br/> <br/>
      <apex:inputCheckbox value="{!Case.IsEscalated}" selected="true" style="display:none;"/>
      <apex:commandButton action="{!save}" value="Submit" />
      
    </apex:form>  
    </apex:pageBlock>   
</apex:page>

Thanks!

 
Amit Chaudhary 8Amit Chaudhary 8
Try to add assert like below
@istest public class EscalationPageExtensionTest {

    public static testMethod void testEscalationPageExtension()
	{

        Apexpages.StandardController stdController = new Apexpages.StandardController(new Case());
        EscalationPageExtension controller = new EscalationPageExtension(stdController);
		
		List<SelectOption> lstPicklist = controller.options;
		
		System.assert(lstPicklist.size() > 0 );
		
        
    }
}

Let us know if this will help you
Glyn Anderson 3Glyn Anderson 3
Isaac,

Instead of setting the checkbox by rendering the corresponding outputPanel (and using a hidded inputCheckbox with selected="true"), you should add an action to each of your actionSupports:  action="{!setDataIntegrity}.  In the controller, the action will set the checkbox according to the new value of the picklist.  You can get rid of the hidden fields on the VF page.  This will also clear the checkbox if the user changes from "Yes" to "No" - which your version won't do.  This design makes the code testable, since you can call the actions from your test class.

Controller changes:  (only one action is shown - others will be similar)

<pre>
    private ApexPages.StandardController ctrl;
    public EscalationPageExtension(ApexPages.StandardController controller)
    {
        ctrl = controller;
    }
    // this action is called by the actionSupport 
    public void setDataIntegrity()
    {
        ctrl.getRecord().Escalation_Data_Integrity__c = picklist1.getValue() == 'true';
    }
</pre>
 
Isaac GomolkaIsaac Gomolka
Thanks for the answers, I've been changing things around to take ur suggestions.

Glyn, I love the idea, could you please give me an example of how the testing for that would look? I'm very bad at creating test classes, so im not sure how i would test those. Thanks a lot, i really appreciate the help!

Thanks,
Isaac
Isaac GomolkaIsaac Gomolka
Also the code doesnt seem to be working for me. It doesnt recognize Escalation_Data_Integrity__c or any of the custom fields. It gives me an error and says variable does not exist. Is there a different way of referencing it? Do i need a pagereference or something? Im not sure why it doesnt recognize it, but its making the whole thing not work.

Thanks for the help!

Isaac
Glyn Anderson 3Glyn Anderson 3
Isaac,

Sorry for the late reply on this.  Try this version:

<pre>
    private ApexPages.StandardController ctrl;
    public EscalationPageExtension(ApexPages.StandardController controller)
    {
        ctrl = controller;
    }
    // this action is called by the actionSupport 
    public void setDataIntegrity()
    {
        Case theCase = (Case) ctrl.getRecord();
        theCase.Escalation_Data_Integrity__c = picklist1.getValue() == 'true';
    }
</pre>

For testing, you need to simulate the action:

<pre>
@isTest
public class EscalationPageExtensionTest
{
    public static testMethod void testEscalationPageExtension()
    {
        Case theCase = new Case();
        Apexpages.StandardController stdController = new Apexpages.StandardController( theCase );
        EscalationPageExtension controller = new EscalationPageExtension(stdController);
        controller.picklist1 = 'true';
        controller.setDataIntegrity();
        System.assert( theCase.Escalation_Data_Integrity__c == true );
        controller.picklist1 = 'false';
        controller.setDataIntegrity();
        System.assert( theCase.Escalation_Data_Integrity__c == false );
    }
}
</pre>