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 for extension, Please!

Hi everyone. I have been trying to figure out this test class for my extension class, but ive been struggling with it for 2 weeks. I have a test class that runs, but I'm not sure if it truly verifies everything its supposed to. I have been told by some that it is enough, and by others I've been told its not good enough and I should have a method for every possibility of combinations of my picklists Yes/No 's( I have 3 picklists). Can somebody please help me by sending me code for what my test class should look like. I've been having trouble with this for so long, I just need to get this down. Thank you so much.

I've already been linked out to all the salesforce pages explaining test classes and test classes with extensions and all that, I'm still confused, so seeing the code would help me a lot more than links. Thank you all so much for the help in advanced!

Extension class:
public with sharing class EscalationPageExtension { 
    
    public String picklist1{get;set;}
    public String picklist2{get;set;}
    public String picklist3{get;set;}
   
    private ApexPages.StandardController ctrl;
    public EscalationPageExtension(ApexPages.StandardController controller)
    {
        ctrl = controller;    
    }
    
    public List<SelectOption> options { get; private set; }
    {
    options = new List<SelectOption>();
    options.add(new SelectOption('', '-Select-')); // ==> option null
    options.add(new SelectOption('false', 'No'));
    options.add(new SelectOption('true', 'Yes'));
    }
    
    
    public void setDataIntegrity()
    {
        Case cas = (Case)ctrl.getRecord();
        cas.Escalation_Data_Integrity__c = Boolean.valueof(picklist1); 
    }
    
    public void setViableWorkaround()
    {
        Case cas = (Case)ctrl.getRecord();
        cas.Escalation_Viable_Workaround__c = Boolean.valueof(picklist2);
    }
    
    public void setAppFunction()
    {
        Case cas = (Case)ctrl.getRecord();
        cas.Escalation_Application_Functionality__c = Boolean.valueof(picklist3);
    }
}

Visualforce Page:
<apex:page standardController="Case" extensions="EscalationPageExtension"><!-- We should keep sidebars and headers to give them a way out -->
    <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" action="{!setDataIntegrity}"/>
          <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" action="{!setViableWorkaround}"/>
          <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" action="{!setAppFunction}"/>
          <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>

My current Test Class:
@isTest 
public class EscalationPageExtensionTest 
{
    static testMethod void testMethod1() 
    {
    
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email');
        insert cas;
        

        Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(cas);
            EscalationPageExtension  testObj = new EscalationPageExtension(sc);
        
            testObj.picklist1 = 'true';
            testObj.picklist2 = 'true';
            testObj.picklist3 = 'true';
            cas.Escalation_Users_Affected__c = '123';
            cas.Escalation_Business_Functionality__c = 'The Business Functionality I cant perform is....';
            
            testObj.setDataIntegrity();
            System.assertEquals(cas.Escalation_Data_Integrity__c, true);
            cas.Escalation_Data_Integrity_Explain__c = 'Affects Data Integrity by...';
            
            testObj.setViableWorkaround();
            System.assertEquals(cas.Escalation_Viable_Workaround__c, true);
            cas.Escalation_Viable_Workaround_Explain__c = 'The Viable Workaround is...';
        
            testObj.setAppFunction();
            System.assertEquals(cas.Escalation_Application_Functionality__c, true);
            cas.Escalation_App_Functionality_Explain__c = 'Affects Application Functionality by...';
            
            List<SelectOption> lstOptions  = testObj.options;
            System.assert(lstOptions.size() > 0 );
            
            sc.save();
            List<Case> lstCase = [select id,Escalation_Application_Functionality__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Application_Functionality__c , true ); 

        Test.StopTest();
    }
}

Again I'm not sure if that test class verifies enough. I have been told to make a seperate method tht runs for every cmobination of picklists to try to test every possibility, but im not sure if that is necessary. Please Help, i really need it. Thank you so much!

Isaac​
Best Answer chosen by Isaac Gomolka
Amit Chaudhary 8Amit Chaudhary 8
yes you can do same in one method as well like below
@isTest 
public class EscalationPageExtensionTest 
{
    static testMethod void testEscalationApplicationFunctionality() 
    {
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email');
        insert cas;
		List<Case> lstCase;
		
        Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(cas);
            EscalationPageExtension  testObj = new EscalationPageExtension(sc);
        
            testObj.picklist1 = 'true';
            testObj.picklist2 = 'true';
            testObj.picklist3 = 'true';
            cas.Escalation_Users_Affected__c = '123';
            cas.Escalation_Business_Functionality__c = 'The Business Functionality I cant perform is....';
            
            List<SelectOption> lstOptions  = testObj.options;
            System.assert(lstOptions.size() > 0 );
			
            testObj.setAppFunction();
            sc.save();
            lstCase = [select id,Escalation_Application_Functionality__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Application_Functionality__c , true ); 

            testObj.setDataIntegrity();
            sc.save();

            lstCase = [select id,Escalation_Data_Integrity__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Data_Integrity__c , true ); 
			
            testObj.setViableWorkaround();
            sc.save();
            lstCase = [select id,Escalation_Viable_Workaround__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Viable_Workaround__c , true ); 

		Test.StopTest();
    }	
}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest 
public class EscalationPageExtensionTest 
{
    static testMethod void testEscalationApplicationFunctionality() 
    {
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email');
        insert cas;

        Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(cas);
            EscalationPageExtension  testObj = new EscalationPageExtension(sc);
        
            testObj.picklist1 = 'true';
            testObj.picklist2 = 'true';
            testObj.picklist3 = 'true';
            cas.Escalation_Users_Affected__c = '123';
            cas.Escalation_Business_Functionality__c = 'The Business Functionality I cant perform is....';
            
            List<SelectOption> lstOptions  = testObj.options;
            System.assert(lstOptions.size() > 0 );
			
            testObj.setAppFunction();
            sc.save();
            List<Case> lstCase = [select id,Escalation_Application_Functionality__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Application_Functionality__c , true ); 

        Test.StopTest();
    }

    static testMethod void testEscalationDataIntegrity() 
    {
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email');
        insert cas;

        Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(cas);
            EscalationPageExtension  testObj = new EscalationPageExtension(sc);
            testObj.picklist1 = 'true';
            testObj.picklist2 = 'true';
            testObj.picklist3 = 'true';
            cas.Escalation_Users_Affected__c = '123';
            cas.Escalation_Business_Functionality__c = 'The Business Functionality I cant perform is....';
            testObj.setDataIntegrity();
            sc.save();

            List<Case> lstCase = [select id,Escalation_Data_Integrity__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Data_Integrity__c , true ); 

        Test.StopTest();
    }

    static testMethod void testEscalationViableWorkaround() 
    {
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email');
        insert cas;

        Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(cas);
            EscalationPageExtension  testObj = new EscalationPageExtension(sc);
            testObj.picklist1 = 'true';
            testObj.picklist2 = 'true';
            testObj.picklist3 = 'true';
            cas.Escalation_Users_Affected__c = '123';
            cas.Escalation_Business_Functionality__c = 'The Business Functionality I cant perform is....';
            
            testObj.setViableWorkaround();
            sc.save();
            List<Case> lstCase = [select id,Escalation_Viable_Workaround__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Viable_Workaround__c , true ); 

        Test.StopTest();
    }
	
}

NOTE:- Above test class including all three method. Let me know if this will help you
 
Isaac GomolkaIsaac Gomolka
Thanks for all the help Amit, I know you've been helping me out a lot. Would I be able to combine those into one method? Could I put all of the "List<Case> lstCase = [select id,Escalation_Application_Functionality__c from case where id =:cas.id ]; System.assert(lstCase.size() > 0 ); System.assertEquals(lstCase[0].Escalation_Application_Functionality__c , true );" lines together in one method?
Amit Chaudhary 8Amit Chaudhary 8
yes you can do same in one method as well like below
@isTest 
public class EscalationPageExtensionTest 
{
    static testMethod void testEscalationApplicationFunctionality() 
    {
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email');
        insert cas;
		List<Case> lstCase;
		
        Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(cas);
            EscalationPageExtension  testObj = new EscalationPageExtension(sc);
        
            testObj.picklist1 = 'true';
            testObj.picklist2 = 'true';
            testObj.picklist3 = 'true';
            cas.Escalation_Users_Affected__c = '123';
            cas.Escalation_Business_Functionality__c = 'The Business Functionality I cant perform is....';
            
            List<SelectOption> lstOptions  = testObj.options;
            System.assert(lstOptions.size() > 0 );
			
            testObj.setAppFunction();
            sc.save();
            lstCase = [select id,Escalation_Application_Functionality__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Application_Functionality__c , true ); 

            testObj.setDataIntegrity();
            sc.save();

            lstCase = [select id,Escalation_Data_Integrity__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Data_Integrity__c , true ); 
			
            testObj.setViableWorkaround();
            sc.save();
            lstCase = [select id,Escalation_Viable_Workaround__c from case where id =:cas.id ];
            System.assert(lstCase.size() > 0 );
            System.assertEquals(lstCase[0].Escalation_Viable_Workaround__c , true ); 

		Test.StopTest();
    }	
}

Let us know if this will help you
 
This was selected as the best answer
Isaac GomolkaIsaac Gomolka
Yes it absolutely helps me. Thanks for everything, I really appreciate it!!