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
B TulasiB Tulasi 

How to write test class for Registration Page

Hi All,

How to write test class for Registration Page

Apex class :

public class RegistrationExtention {
    public Registration__c reg { get; set;}
    //set<id> s=new set<id>
    public RegistrationExtention(ApexPages.StandardController controller) {
        reg=new Registration__c();
    }
        public PageReference dosave(){       
       
            if  (reg.Name==null || reg.Name.length()==0){
                //reg.Name.addError('Last Name will not be blank');
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO, 'Last Name will not be blank'));
                return null;
            }
            else if (reg.Mail_Id__c==null || reg.Mail_Id__c.length()==0){
                //reg.Mail_Id__c.addError('Email will not be blank');
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO, 'Email will not be blank'));
                return null;
            }
       
            else if(reg.User_Name__c==null|| reg.User_Name__c.length()==0){
                //reg.User_Name__c.addError('UserwName will not be blank');
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO, 'UserName will not be blank'));
                return null;
            }
            else if(reg.Password__c == null || reg.Password__c.length()==0) {
                //reg.Password__c.addError('Password will not be blank');
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO, 'Password will not be blank'));
                return null;            
            }
            else if(reg.Confirm_Password__c == null || reg.Confirm_Password__c.length()==0) {
                //reg.Confirm_Password__c.addError('Confirm Password will not be blank');
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO, 'Confirm Password will not be blank'));
                return null;            
            }                
                
            else{   
        insert reg;
        PageReference pr=new PageReference ('/apex/loginpage');
        pr.setredirect(true);
        return pr;
        }
        }  

}


Test Class :

@isTest
public class RegistrationExtentionTest{
    public static testMethod void regpage(){
ApexPages.Message[] pageMessages = ApexPages.getMessages();

    
    Registration__c reg1=new Registration__c();
       ApexPages.StandardController controller=new ApexPages.StandardController(reg1);
        RegistrationExtention re=new RegistrationExtention(controller);
        re.reg.Name='';
        re.reg.Mail_Id__c='';
        re.reg.User_Name__c='';
        re.reg.Password__c='';
        re.reg.Confirm_Password__c='';    
        
        
        re.dosave();

    }   

}

It's covering only 7 lines out of 23. How to cover the if ,  else if statements and need to cover insert operation too.

Thanks in advance
Tulasi
Best Answer chosen by B Tulasi
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class.
@isTest
public class RegistrationExtentionTest
{
    public static testMethod void regpage()
	{
    
		Registration__c reg1=new Registration__c();
        ApexPages.StandardController controller=new ApexPages.StandardController(reg1);
        RegistrationExtention re=new RegistrationExtention(controller);
        re.dosave();
		
        re.reg.Name='Something';
        re.dosave();

        re.reg.Mail_Id__c='abc@gmail.com';
        re.dosave();

        re.reg.User_Name__c='Something';
        re.dosave();

        re.reg.Password__c='Something';
        re.dosave();

        re.reg.Confirm_Password__c='Something';  
        re.dosave();
        

    }
    

}