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
aressaress 

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;
    }
    
}
Raj VakatiRaj Vakati
Try this code
 
@isTest
private class StudentExtesionTest {

     
    static TestMethod void StudentExtesionTest_UnitTest(){
        Test.startTest();
           Student__c sr = new Student__c();
		   sr.Name='Test';
		   // insert other fields
		   insert sr ; 
            PageReference pageRef = Page.YOURPAGENAME;
            Test.setCurrentPage(pageRef);
             
            ApexPages.StandardController sc = new ApexPages.StandardController(sr);
            StudentRegFormExtension  ext = new StudentRegFormExtension (sc);
             ext.save();
        Test.stopTest();
    }

    
}

 
Raj VakatiRaj Vakati
@isTest
private class StudentExtesionTest {

     
    static TestMethod void StudentExtesionTest_UnitTest(){
        Test.startTest();
           Student__c sr = new Student__c();
		   sr.Name='Test';
		   // insert other fields
		   insert sr ; 
            PageReference pageRef = Page.YOURPAGENAME;
            Test.setCurrentPage(pageRef);
             
            ApexPages.StandardController sc = new ApexPages.StandardController(sr);
            StudentRegFormExtension  ext = new StudentRegFormExtension (sc);
             ext.save();
        Test.stopTest();
    }

     static TestMethod void StudentExtesionTest_Failure(){
        Test.startTest();
		try{
           Student__c sr = new Student__c();
		   //sr.Name='';
		   // insert other fields
		   insert sr ; 
            PageReference pageRef = Page.YOURPAGENAME;
            Test.setCurrentPage(pageRef);
             ApexPages.currentPage().getParameters().put('Success','Success');

            ApexPages.StandardController sc = new ApexPages.StandardController(sr);
            StudentRegFormExtension  ext = new StudentRegFormExtension (sc);
             ext.save();
		}catch(Exception e){
		}
        Test.stopTest();
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
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
@isTest
private class StudentRegFormExtTest {

     
    static TestMethod void Student_UnitTest()
	{
		Student__c stud = new Student__c();
		stud.Name ='Test';
		Test.startTest();
			
			ApexPages.currentPage().getParameters().put('Success','true');
			ApexPages.StandardController sc = new ApexPages.StandardController(stud);
			StudentRegFormExtension  ext = new StudentRegFormExtension(sc);
			ext.save();
			List<Student__c> lstStud = [Select id from Student__c];
			System.assert(lstStud != null);
		Test.stopTest();
    }
}

Let us know if this will help you