You need to sign in to do that
Don't have an account?
B Tulasi
How to write a test class for apex error message in Apex class
Hi All,
How to write a test class for apex error message in Apex class.
Apex Class :
else if (reg.Mail_Id__c==null || reg.Mail_Id__c.length()==0){
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO, 'Email will not be blank'));
return null;
}
Test Class :
Registration__c reg1=new Registration__c();
ApexPages.StandardController controller=new ApexPages.StandardController(reg1);
RegistrationExtention re=new RegistrationExtention(controller);
re.reg.Name='something';
re.reg.Mail_Id__c='abc@gmail.com';
re.reg.User_Name__c='something';
re.reg.Password__c='something';
re.reg.Confirm_Password__c='something';
PageReference pageRef = Page.RegistrationPage;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters();
String id = ApexPages.currentPage().getParameters().get('id');
system.assertEquals(true,id!=null);
re.dosave();
thanks in advance
Thulasi
How to write a test class for apex error message in Apex class.
Apex Class :
else if (reg.Mail_Id__c==null || reg.Mail_Id__c.length()==0){
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.INFO, 'Email will not be blank'));
return null;
}
Test Class :
Registration__c reg1=new Registration__c();
ApexPages.StandardController controller=new ApexPages.StandardController(reg1);
RegistrationExtention re=new RegistrationExtention(controller);
re.reg.Name='something';
re.reg.Mail_Id__c='abc@gmail.com';
re.reg.User_Name__c='something';
re.reg.Password__c='something';
re.reg.Confirm_Password__c='something';
PageReference pageRef = Page.RegistrationPage;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters();
String id = ApexPages.currentPage().getParameters().get('id');
system.assertEquals(true,id!=null);
re.dosave();
thanks in advance
Thulasi
All Answers
Thanks for ur reply.
Before that, I tried that thing. But That was not working. After that I have written in the above code.
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='Something';
re.reg.Mail_Id__c='abc@gmail.com';
re.reg.User_Name__c='Something';
re.reg.Password__c='Something';
re.reg.Confirm_Password__c='Something';
/* PageReference pageRef = Page.RegistrationPage;
//pageRef.getParameters().put('id', String.valueOf(re.Id));
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters();
String id = ApexPages.currentPage().getParameters().get('id');
system.assertEquals(true,id!=null); */
// re.getaccountContact();
re.dosave();
}
/* public static testMethod void regpage1(){
Registration__c reg2=new Registration__c();
ApexPages.StandardController controller1=new ApexPages.StandardController(reg2);
RegistrationExtention re1=new RegistrationExtention(controller1);
re1.reg.Name='';
re1.reg.Mail_Id__c='';
re1.reg.User_Name__c='';
re1.reg.Password__c='';
re1.reg.Confirm_Password__c='';
re1.dosave();
} */
}