You need to sign in to do that
Don't have an account?
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
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

Please try below test class.