You need to sign in to do that
Don't have an account?

Test classes for saving record using VF PAGE
Hello Experts,
Need test class for following VF page that display a registeration form with name,roll number,gender fields. all save the result and display form in multiple language.
--------------------------VF PAGE-------------------------------
<apex:page standardController="Student__c" extensions="StudentRegFormExtension" language="{!selectedLang }"
sidebar="false"
>
<apex:form >
<apex:selectList value="{!selectedLang}" size="1">
<apex:selectoptions value="{!listOfLang}"/>
<apex:actionsupport event="onchange" reRender="form"/>
</apex:selectlist>
<apex:outputPanel id="pageMessage">
<apex:pageMessages rendered="true" ></apex:pageMessages>
</apex:outputPanel>
<apex:pageblock >
<apex:pageblocksection id="form">
<apex:inputfield value="{! Student__c.Name }"/>
<apex:inputfield value="{! Student__c.Roll_Number__c }"/>
<apex:inputfield value="{! Student__c.Gender__c }"/>
<apex:inputfield value="{! Student__c.Course_Applying_For__c }"/>
<apex:inputfield value="{! Student__c.State__c }"/>
<apex:inputfield value="{! Student__c.Country__c }"/>
</apex:pageblocksection>
<div align="center" draggable="false" >
<apex:commandButton value="{!$Label.Record_Inserted}" action="{! save}" reRender="pageMessage"/>
</div>
</apex:pageblock>
</apex:form>
</apex:page>
-------------------------------------------Controller------------------------------------------
public class StudentRegFormExtension {
public Boolean showMessage{get;set;}
public String selectedLang{get;set;}
public List<SelectOption> listOfLang {get;set;}
public ApexPages.StandardController controller{get;set;}
public StudentRegFormExtension(ApexPages.StandardController con) {
controller = con;
showMessage = false;
listOfLang = new List<SelectOption>();
listOfLang.add(new SelectOption('en','English'));
listOfLang.add(new SelectOption('es','Spanish'));
listOfLang.add(new SelectOption('fr','French'));
if(ApexPages.currentPage().getParameters().get('Success') != null ) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,
System.Label.Record_Inserted));
}
}
public PageReference save() {
Student__c student = (Student__c)controller.getRecord();
PageReference pr;
if(student.Name == null) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,
System.Label.Error_Inserting_Record));
}else {
controller.save();
pr = new PageReference('/apex/StudentRegForm?Success=true');
pr.setRedirect(true);
}
return pr;
}
}
Need test class for following VF page that display a registeration form with name,roll number,gender fields. all save the result and display form in multiple language.
--------------------------VF PAGE-------------------------------
<apex:page standardController="Student__c" extensions="StudentRegFormExtension" language="{!selectedLang }"
sidebar="false"
>
<apex:form >
<apex:selectList value="{!selectedLang}" size="1">
<apex:selectoptions value="{!listOfLang}"/>
<apex:actionsupport event="onchange" reRender="form"/>
</apex:selectlist>
<apex:outputPanel id="pageMessage">
<apex:pageMessages rendered="true" ></apex:pageMessages>
</apex:outputPanel>
<apex:pageblock >
<apex:pageblocksection id="form">
<apex:inputfield value="{! Student__c.Name }"/>
<apex:inputfield value="{! Student__c.Roll_Number__c }"/>
<apex:inputfield value="{! Student__c.Gender__c }"/>
<apex:inputfield value="{! Student__c.Course_Applying_For__c }"/>
<apex:inputfield value="{! Student__c.State__c }"/>
<apex:inputfield value="{! Student__c.Country__c }"/>
</apex:pageblocksection>
<div align="center" draggable="false" >
<apex:commandButton value="{!$Label.Record_Inserted}" action="{! save}" reRender="pageMessage"/>
</div>
</apex:pageblock>
</apex:form>
</apex:page>
-------------------------------------------Controller------------------------------------------
public class StudentRegFormExtension {
public Boolean showMessage{get;set;}
public String selectedLang{get;set;}
public List<SelectOption> listOfLang {get;set;}
public ApexPages.StandardController controller{get;set;}
public StudentRegFormExtension(ApexPages.StandardController con) {
controller = con;
showMessage = false;
listOfLang = new List<SelectOption>();
listOfLang.add(new SelectOption('en','English'));
listOfLang.add(new SelectOption('es','Spanish'));
listOfLang.add(new SelectOption('fr','French'));
if(ApexPages.currentPage().getParameters().get('Success') != null ) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,
System.Label.Record_Inserted));
}
}
public PageReference save() {
Student__c student = (Student__c)controller.getRecord();
PageReference pr;
if(student.Name == null) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,
System.Label.Error_Inserting_Record));
}else {
controller.save();
pr = new PageReference('/apex/StudentRegForm?Success=true');
pr.setRedirect(true);
}
return pr;
}
}
1) https://trailhead.salesforce.com/modules/apex_testing
Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
NOTE:- Always add Assert in Test Class.
Sample Test Class for you
Let us know if this will help you