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
Krish NKrish N 

Need help with test class for vf page controller

Hello everyone, I'm new to Salesforce. I would appreciate if any one help me how to write test class for my visualforce pages. Below is the controller. I have two visualforce pages QRcase (output) and QRcaseEdit (input/edit). These pages are used to create cases for service related issues of our customers. Could any one please help me with the logic. Thank you!
public class QRreportController

    
   
   
/* --------------------- variable for the standard controller ------------------------------- */ 
{
    private ApexPages.StandardController std;        
/* --------------------- variable for this case ------------------------------- */     
    public Case c {get; set;}

/*  -------------------- standard work order controller  ----------------------------  */            
    public QRreportController (ApexPages.StandardController stdCtrl)
    {
        // prepopulates some fields on page load
        this.c = (Case)stdCtrl.getRecord();
        Map<ID,Schema.RecordTypeInfo> rt_Map = Case.sObjectType.getDescribe().getRecordTypeInfosById();
        Id RecordTypeIdCase = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Quality Report Case').getRecordTypeId();
        Id rtId = RecordTypeIdCase;
        c.RecordTypeId=rtId;
        c.AccountId=System.currentPageReference().getParameters().get('AccountId');
        std=stdCtrl;
    }
    
    
      
          
    
    
    
/* --------------------- Get Case ------------------------------- */     
    public Case getCase()
    {       
     return (Case) std.getRecord(); 
    }

  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

/* --------------------- Save and Exit method ------------------------------- */
    public PageReference saveAndExit()
    {
        std.save();
        PageReference pageRef1 = new PageReference('/' + getCase().id);
        return pageRef1;
    }
/* --------------------- Edit method ------------------------------- */
    public PageReference edit1()
    {
        PageReference pageRef2 = Page.QRcaseEdit;
        pageRef2.getParameters().put('id', getCase().id);
        pageRef2.getParameters().put('AccountId', getCase().AccountId);
        return pageRef2;
    }
    

/* --------------------- Quick Save method ------------------------------- */
public PageReference save()
    {
        std.save();
        PageReference pageRef2 = Page.QRcase;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'SUCCESS!  Changes saved.'));
    
        pageRef2.getParameters().put('id', getCase().id);
        pageRef2.getParameters().put('AccountId',getCase().AccountId);
        return pageRef2;

    }
   
  

    public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = getCase().Id ;// the record the file is attached to
    attachment.IsPrivate = true;

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }
  
}
Best Answer chosen by Krish N
Raj VakatiRaj Vakati
Try this code

 
@isTest

public class QRreportControllerTest {

static testMethod void testCCQQ () {


Id rtId = [select Id, name from RecordType where name = 'My Record Type' and SObjectType = 'Case' limit 1].Id;


//create account
Account acc = new Account();
//enter details  
acc.Name = 'Test Account';
acc.Corporate_Member__c = TRUE;
acc.Corporate_Account_Link__c = '001W0000004Hu64';
insert acc;


Id RecordTypeIdCase = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Quality Report Case').getRecordTypeId();

//create case
Case c = new Case();
//enter details
c.AccountId = acc.Id;
c.ContactId = personContact;
c.RecordTypeId = RecordTypeIdCase;
c.Type = 'My Type';
c.Origin = 'My Origin';
c.Status = 'My Status';
c.Call_Attempt__c = '1';    
insert c;

    
				
				PageReference pageRef = Page.YOURPAGE_Name;
pageRef.getParameters().put('Id',c.Id);
pageRef.getParameters().put('AccountId',acc.Id);

Test.setCurrentPageReference(pageRef);

 ApexPages.StandardController sc = new ApexPages.StandardController(c);
                QRreportController controller = new QRreportController(sc);

				controller.getCase();
				controller.saveAndExit();
				controller.edit1();
				controller.upload();
				controller.save();
				controller.getCase();
				

}

}

 

All Answers

Raj VakatiRaj Vakati
Try this code

 
@isTest

public class QRreportControllerTest {

static testMethod void testCCQQ () {


Id rtId = [select Id, name from RecordType where name = 'My Record Type' and SObjectType = 'Case' limit 1].Id;


//create account
Account acc = new Account();
//enter details  
acc.Name = 'Test Account';
acc.Corporate_Member__c = TRUE;
acc.Corporate_Account_Link__c = '001W0000004Hu64';
insert acc;


Id RecordTypeIdCase = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Quality Report Case').getRecordTypeId();

//create case
Case c = new Case();
//enter details
c.AccountId = acc.Id;
c.ContactId = personContact;
c.RecordTypeId = RecordTypeIdCase;
c.Type = 'My Type';
c.Origin = 'My Origin';
c.Status = 'My Status';
c.Call_Attempt__c = '1';    
insert c;

    
				
				PageReference pageRef = Page.YOURPAGE_Name;
pageRef.getParameters().put('Id',c.Id);
pageRef.getParameters().put('AccountId',acc.Id);

Test.setCurrentPageReference(pageRef);

 ApexPages.StandardController sc = new ApexPages.StandardController(c);
                QRreportController controller = new QRreportController(sc);

				controller.getCase();
				controller.saveAndExit();
				controller.edit1();
				controller.upload();
				controller.save();
				controller.getCase();
				

}

}

 
This was selected as the best answer
Krish NKrish N
Hello Raj Vakati, Thank you for the help. So as I said, I have two vf pages one for input and the other for output. Which page should I call  for this:
PageReference pageRef = Page.YOURPAGE_Name; Below is my input vf page: 
<apex:page id="qr" standardController="Case" sidebar=""
            extensions="QRreportController"
           tabStyle="Case" title="Quality Report" >

<style>
    .bPageBlock .detailList .labelCol { width: 1%; min-width:180px; }
    .bPageBlock .detailList .data2Col { width: 99%; }
</style>
<style>
        .parent {
        background-color: #82E0AA !important;
        }
        .lock {
        background-color: #FF0101 !important;
        }
</style>
<style type="text/css">
            [id*=troy] {font-size:150%;}
            [id*=lock] {font-size:100%;}
        </style>

        <style type="text/css">
            [id*=bold] { font-weight: bold;}
</style>

<chatter:feedwithfollowers entityId="{!Case.Id}"/>

<apex:form >
<apex:pagemessages ></apex:pagemessages> <!-- displays all message generated by the component -->
<apex:sectionHeader subtitle="New Quality Report" title="Case Edit"/>

<apex:pageBlock title="QR Case Detail" mode="edit">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>

        <apex:commandButton action="{!saveAndExit}" value="Save & Exit"/>   
          <apex:commandButton action="{!cancel}" value="Cancel"/>   
      </apex:pageBlockButtons>

    <apex:pageBlockSection showHeader="true" columns="3" id="block1" title="QR Overview">
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Case_Number__c.inlineHelpText}"><apex:outputLabel value="QR Case Number" for="QR_Case_Number__c"/><apex:outputText value="{!Case.QR_Case_Number__c}" id="QR_Case_Number__c" style="width:50%;" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Status.inlineHelpText}"><apex:outputLabel value="Status" for="Status"/><apex:outputText value="{!Case.Status}" id="Status" style="width:50%;" /></apex:pageBlockSectionItem>  
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Case_Age__c.inlineHelpText}"><apex:outputLabel value="Age (Days)" for="Case_Age__c"/><apex:inputField value="{!Case.Case_Age__c}" id="Case_Age__c" style="width:50%;" /></apex:pageBlockSectionItem> 
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.CreatedDate.inlineHelpText}"><apex:outputLabel value="Date Initiated" for="CreatedDate"/><apex:inputField value="{!Case.CreatedDate}" id="CreatedDate" style="width:50%;" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.ClosedDate.inlineHelpText}"><apex:outputLabel value="Date Closed" for="ClosedDate"/><apex:inputField value="{!Case.ClosedDate}" id="ClosedDate" style="width:50%;" /></apex:pageBlockSectionItem>
    </apex:pageBlockSection>
    
       
<apex:pageBlockSection showHeader="true" columns="1" id="block2" title="Customer Information">
        
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Account__c.inlineHelpText}"><apex:outputLabel value="Customer Name" for="Account__c"/><apex:inputField value="{!Case.Account__c}" id="Account__c" required="true" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Site_Name__c.inlineHelpText}"><apex:outputLabel value="Site Name" for="Site_Name__c"/><apex:inputField value="{!Case.Site_Name__c}" id="Site_Name__c"  /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Street_Address__c.inlineHelpText}"><apex:outputLabel value="Street Address" for="Street_Address__c"/><apex:inputField value="{!Case.Street_Address__c}" id="Street_Address__c" style="width:8.1%;" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Site_City__c.inlineHelpText}"><apex:outputLabel value="City" for="Site_City__c"/><apex:inputField value="{!Case.Site_City__c}" id="Site_City__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Site_State__c.inlineHelpText}"><apex:outputLabel value="State" for="Site_State__c"/><apex:inputField value="{!Case.Site_State__c}" id="Site_State__c" required="true" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Site_Country__c.inlineHelpText}"><apex:outputLabel value="Country" for="Site_Country__c"/><apex:inputField value="{!Case.Site_Country__c}" id="Site_Country__c" required="true"  /></apex:pageBlockSectionItem>   
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Site_Region__c.inlineHelpText}"><apex:outputLabel value="Territory" for="Site_Region__c"/><apex:inputField value="{!Case.Site_Region__c}" id="Site_Region__c" style="width:6.8%;" required="true" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Zip__c.inlineHelpText}"><apex:outputLabel value="Zip" for="Zip__c"/><apex:inputField value="{!Case.Zip__c}" id="Zip__c" style="width:5%;" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Zone_Tech_Manager__c.inlineHelpText}"><apex:outputLabel value="Zone Technical Manager" for="Zone_Tech_Manager__c"/><apex:inputField value="{!Case.Zone_Tech_Manager__c}" id="Zone_Tech_Manager__c" required="true"/></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Can_we_Contact_Customer__c.inlineHelpText}"><apex:outputLabel value="Can we Contact Customer?" for="Can_we_Contact_Customer__c"/><apex:inputField value="{!Case.Can_we_Contact_Customer__c}" id="Can_we_Contact_Customer__c" /> </apex:pageBlockSectionItem>  
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Customer_Contact_Name__c.inlineHelpText}"><apex:outputLabel value="Customer Contact Name" for="Customer_Contact_Name__c"/><apex:inputField value="{!Case.Customer_Contact_Name__c}" id="Customer_Contact_Name__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Customer_Contact_Number__c.inlineHelpText}"><apex:outputLabel value="Customer Contact Number" for="Customer_Contact_Number__c"/><apex:inputField value="{!Case.Customer_Contact_Number__c}" id="Customer_Contact_Number__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Customer_Contact_Email__c.inlineHelpText}">  <apex:outputLabel value="Customer Contact Email" for="Customer_Contact_Email__c"/><apex:inputField value="{!Case.Customer_Contact_Email__c}" id="Customer_Contact_Email__c" /></apex:pageBlockSectionItem>
    </apex:pageBlockSection> 
            
            
    <apex:pageBlockSection showHeader="true" columns="1" id="block4" title="Customer Issue">
        <apex:pageBlockSectionItem HelpText="Describe the issue the customer is experiencing."><apex:outputLabel value="Description" for="Description"/><apex:inputField value="{!Case.Description}" id="Description"  required="true" style="width:20%;"  /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Action_Taken__c.inlineHelpText}"><apex:outputLabel value="Initial Action Taken" for="QR_Action_Taken__c"/><apex:inputField value="{!Case.QR_Action_Taken__c}" id="QR_Action_Taken__c" required="true" style="width:20%;" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Problem_Type__c.inlineHelpText}"><apex:outputLabel value="Problem Type" for="QR_Problem_Type__c"/><apex:inputField value="{!Case.QR_Problem_Type__c}" id="QR_Problem_Type__c"  required="true" style="width:17.5%;"/></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Component_Type__c.inlineHelpText}"><apex:outputLabel value="Component Type" for="QR_Component_Type__c"/><apex:inputField value="{!Case.QR_Component_Type__c}" id="QR_Component_Type__c" required="true" style="width:17.5%;"/></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Occurrence_Rate__c.inlineHelpText}"><apex:outputLabel value="Occurrence Rate" for="QR_Occurrence_Rate__c"/><apex:inputField value="{!Case.QR_Occurrence_Rate__c}" id="QR_Occurrence_Rate__c" required="true" style="width:17.5%;" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Severity__c.inlineHelpText}"><apex:outputLabel value="Severity" for="QR_Severity__c"/><apex:inputField value="{!Case.QR_Severity__c}" id="QR_Severity__c" required="true" style="width:17.5%;" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Other_comments__c.inlineHelpText}"><apex:outputLabel value="Other Comments" for="Other_comments__c"/><apex:inputField value="{!Case.Other_comments__c}" id="Other_comments__c" style="width:20%;" /></apex:pageBlockSectionItem>

    </apex:pageBlockSection>
    
          
    <apex:pageBlockSection showHeader="true" columns="1" id="block5" title="BUNN Contacts and Assignments" rendered="{!IF((Case.Status = '') || (Case.Status = 'Initiated'), false,true)}" >
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.CreatedById.inlineHelpText}"><apex:outputLabel value="Initiator" for="CreatedById"/><apex:inputField value="{!Case.CreatedById}" id="CreatedById" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Zone_Technical_Manager__c.inlineHelpText}"><apex:outputLabel value="Zone Technical Manager" for="Zone_Technical_Manager__c"/><apex:outputField value="{!Case.Zone_Technical_Manager__c}" id="Zone_Technical_Manager__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Local_Sales_Rep__c.inlineHelpText}"  > <apex:outputLabel value="Local Sales Rep" for="QR_Local_Sales_Rep__c"/><apex:inputField value="{!Case.QR_Local_Sales_Rep__c}" id="QR_Local_Sales_Rep__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Account_Sales_Manager__c.inlineHelpText}"><apex:outputLabel value="Account Sales Manager" for="QR_Account_Sales_Manager__c"/><apex:inputField value="{!Case.QR_Account_Sales_Manager__c}" id="QR_Account_Sales_Manager__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Coordinator__c.inlineHelpText}"><apex:outputLabel value="Coordinator" for="QR_Coordinator__c"/><apex:inputField value="{!Case.QR_Coordinator__c}" id="QR_Coordinator__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Department__c.inlineHelpText}"><apex:outputLabel value="Owner Department" for="Owner_Dept__c"/><apex:inputField value="{!Case.Department__c}" id="Owner_Dept__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.Dept_Sponsor__c.inlineHelpText}"><apex:outputLabel value="Sponsor" for="Dept_Sponsor__c"/><apex:outputField value="{!Case.Dept_Sponsor__c}" id="Dept_Sponsor__c" /></apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.QR_Owner__c.inlineHelpText}"><apex:outputLabel value="Assigned Owner" for="QR_Owner__c"/><apex:inputField value="{!Case.QR_Owner__c}" id="QR_Owner__c" /></apex:pageBlockSectionItem>

    </apex:pageBlockSection> 
       
           

    
    </apex:pageBlock>
   </apex:form>
   
    <apex:relatedList rendered="{!IF((Case.Status = '') || (Case.Status = 'Initiated'), false,true)}" list="Affected_Serial_Numbers__r"  />
    <apex:relatedList rendered="{!IF((Case.QR_Reference_QR__c = ''), false,true)}"  list="AssociatedQRCases__r"/>
    <apex:relatedList list="ProcessSteps"  />
    <apex:relatedList list="CombinedAttachments" />

<apex:form >
<apex:pageBlock title="System Information">
    <apex:pageBlockSection showHeader="false" columns="2" id="block1">
        <apex:outputfield value="{!case.CreatedById}"/>
        <apex:outputfield value="{!case.LastModifiedById}"/>
        <apex:pageBlockSectionItem HelpText="{!$ObjectType.case.fields.CreatedDate.inlineHelpText}"><apex:outputLabel value="Created Date" for="CreatedDate"/><apex:inputField value="{!Case.CreatedDate}" id="CreatedDate"/></apex:pageBlockSectionItem>
        <apex:outputfield value="{!case.LastModifiedDate}"/>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>