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
BryanTSCBryanTSC 

Custom Controller Test Class

Despite much researching and reading, I'm not quite grasping the controller test class documentation or examples.  If anyone can help me get going with a test class for this, I would greatly appreciate it!

 

Controller:

 

public class newInquiryController {
   
   Account account;
   Contact contact;
   Inquiry_History__c inquiry;
    

   public Contact getContact() {
      if(contact == null) contact = new Contact();
      return contact;
   }
   
   public Inquiry_History__c getInquiry() {
      if(inquiry == null) inquiry = new Inquiry_History__c();
      return inquiry;
   }
    
    
   public PageReference step1() {
      return Page.newInquiryStep1;
   }

   public PageReference step2() {
      return Page.newInquiryStep2;
   } 
    
   public PageReference save() {
    
      if(inquiry.XCollege__c == 'SCA') {
      contact.XOrigin__c='a0Q80000000OVUH';
      }
      if(inquiry.XCollege__c == 'RSC') {
      contact.XOrigin__c='a0Q80000000OVWP';
      }
      if(inquiry.XCollege__c == 'SAW') {
      contact.XOrigin__c='a0Q80000000OVQv';
      }
      if(inquiry.XCollege__c == 'SGS') {
      contact.XOrigin__c='a0Q80000000OVSu';
      }
      contact.XAddress_Type__c='Mailing; Residence';
      insert contact;
      inquiry.XContact__c=contact.id;
      insert inquiry;

    
PageReference pageRef = new PageReference('http://www.returnurl.com/thankyou.php');

return pageRef;
   }

}

 

 

Page:

 

<apex:page controller="newInquiryController" showHeader="FALSE" sidebar="FALSE">
<apex:stylesheet value="{!$Resource.StyleSheet}"/>
<apex:form >
<table border="0" width="650px" cellspacing="0" align="center">

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>First Name</td>
<td width="65%"><apex:inputField id="contactFirstName" value="{!contact.firstName}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Last Name</td>
<td width="65%"><apex:inputField id="contactLastName" value="{!contact.lastName}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%" valign="top"><font color="red">*</font>Address</td>
<td width="65%"><apex:inputField id="contactAddress" value="{!contact.MailingStreet}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>City</td>
<td width="65%"><apex:inputField id="contactCity" value="{!contact.MailingCity}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>State</td>
<td width="65%"><apex:inputField id="contactState" value="{!contact.MailingState}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Zip Code</td>
<td width="65%"><apex:inputField id="contactZip" value="{!contact.MailingPostalCode}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%">Country</td>
<td width="65%"><apex:inputField id="contactCountry" value="{!contact.MailingCountry}"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%">Mobile Phone</td>
<td width="65%"><apex:inputField id="contactCel" value="{!contact.MobilePhone}"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%">Home Phone</td>
<td width="65%"><apex:inputField id="contactHomePhone" value="{!contact.homePhone}"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Email</td>
<td width="65%"><apex:inputField id="contactEmail" value="{!contact.Email}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Gender</td>
<td width="65%"><apex:inputField id="contactGender" value="{!contact.TargetX_SRMb__Gender__c}" required="TRUE"/></td>
</tr>
<tr>
<td width="5%"></td>
<td width="30%">Birthdate <i>(mm/dd/yyyy)</i></td>
<td width="65%"><apex:inputField id="contactDOB" value="{!contact.Birthdate}"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Current/Most Recent School</td>
<td width="65%"><apex:inputField id="contactSchool" value="{!contact.AccountId}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%">Graduation Year</td>
<td width="65%"><apex:inputField id="contactGradYear" value="{!contact.TargetX_SRMb__Graduation_Year__c}"/></td>
</tr>

<tr height="25px">
<td width="5%"></td>
<td width="30%"></td>
<td width="65%"></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>College of Interest</td>
<td width="65%"><apex:inputField id="inquiryCollege" value="{!inquiry.XCollege__c}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Program</td>
<td width="65%"><apex:inputField id="inquiryMajor" value="{!inquiry.XAnticipated_Major__c}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Start Term</td>
<td width="65%"><apex:inputField id="inquiryStartTerm" value="{!inquiry.XStart_Term__c}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Start Year</td>
<td width="65%"><apex:inputField id="inquiryStartYear" value="{!inquiry.XStart_Year__c}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Entry Type</td>
<td width="65%"><apex:inputField id="inquiryStudentType" value="{!inquiry.XStudent_Type__c}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Intended Load</td>
<td width="65%"><apex:inputField id="inquiryEnrollmentStatus" value="{!inquiry.XAnticipated_Enrollment_Status__c}" required="TRUE"/></td>
</tr>

<tr>
<td width="5%"></td>
<td width="30%"><font color="red">*</font>Residency</td>
<td width="65%"><apex:inputField id="inquiryResidencyStatus" value="{!inquiry.XAnticipated_Housing__c}" required="TRUE"/></td>
</tr>

<tr height="25px">
<td width="5%"></td>
<td width="30%"></td>
<td width="65%" align="right" valign="bottom"><apex:commandButton action="{!step2}" value="Preview" styleClass="btn"/></td>
</tr>
</table>
  </apex:form>


<style type="text/css">
.datePicker, .datePicker .dateBar ,.datePicker .calBody, .datePicker .calDays, .datePicker .buttonBar, .dateFormat, .datePicker Select
{
visibility:hidden;
}
</style>

</apex:page>

 

Step 2 simply displays the data entered on the form and provides a "back" button to go back and edit the information and a "submit" button to execute the save() action.

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

The purpose of testing code is to ensure that your logic will work when deployed to any given organization. You should therefore write a test to ensure that the behavior is what you expect. If you never plan on exporting it to another organization, the way your code is written is fine. However, it's generally considered bad form to hard-code ID values (since they would change in each different organization). Regardless, your test would probably look like this:

 

 

public class newInquiryController {
// your code goes here

// ...

// Now, test the code
  public static testMethod void test() {
    Test.setCurrentPage(Page.newInquiry);
    newInquiryController controller = new newInquiryController();
    controller.XCollege__c = 'SCA';
    System.assertEquals(Page.newInquiryStep1.getUrl(),controller.step1().getUrl(),'Wrong page returned.');
    System.assertEquals(Page.newInquiryStep2.getUrl(),controller.step2().getUrl(),'Wrong page returned.');
    System.assert(controller.getContact()<>null,'Expected a contact object.');
    PageReference result = controller.save();
    System.assertEquals('http://www.returnurl.com/thankyou.php',
       result.getUrl(),'Expected thank you page.');
    System.assertEquals('a0Q80000000OVUH',
       controller.inquiry.XOrigin__c,'Wrong record selected.');
    System.assert(controller.inquiry.XContact__c<>null,
       'Contact failed to create.');
    System.assert(controller.inquiry.Id<>null,
       'Inquiry failed to create.');
  }

 

 

As a side note, you would get better code coverage by using a Map to associate the possible values to the respective ID values. In theory, the best way to do this would be with a query that populates possible options with their respective ID values. It should be possible to achieve 100% coverage with your code.