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

Test class to test vf page that take input and add a record
Need Test Class for following scenario..Where VF Page display a form that take input and add record in Student object.
Please consider all positive negative test cases.
---------------------------------------------------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.SSC__c }"/>
<apex:inputfield value="{! Student__c.HSC__c }"/>
<apex:inputfield value="{! Student__c.City__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;
}
}
Please consider all positive negative test cases.
---------------------------------------------------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.SSC__c }"/>
<apex:inputfield value="{! Student__c.HSC__c }"/>
<apex:inputfield value="{! Student__c.City__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;
}
}
This blog will be very helpful for you to write your expected test class. Follow the below link:
http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html?m=1
Plz let me know if steel have doubt or problems.
If it solves ur problem. Mark it ur ans to help others.