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

How can i define test class for error method
Hi All ,
I tried test class for Error (Both DisplayErrorMsg and Checkmail) method but it didn't work.
Apex class
public void DisplayErrorMsg(){
String phone = con.Prod_Mobile__c;
system.debug('@@phone'+phone);
if(phone.length() == 8 && (phone.startsWith('6') || phone.startsWith('8') || phone.startsWith('9'))){
system.debug('if loop');
//ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Invalid Contact No. Please enter a valid mobile no (8 digits, starts with 6, 8 or 9)'));
}
else{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Check your phoine'));
system.debug('else loop');
}
}
public void checkEmail(){
string emailVal = con.Prod_Email__c;
system.debug('@@email'+emailVal);
if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}[.]{0,1}[a-zA-Z]{0,2}', emailVal))
{
EmailerrorMessage = 'Invalid email address entered.';
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Check your email'));
}
}
}
test class :
@isTest
public class ContactUsThankYouControllerTest {
static testMethod void TestMethod2()
{
ContactUs_Enquiry__c con = new ContactUs_Enquiry__c();
insert con;
con = [select Id,Name from ContactUs_Enquiry__c where id = : con.id];
PageReference pageRef = Page.ContactUsMain;
Test.setCurrentPage(pageRef);
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(con);
ApexPages.currentPage().getParameters().put('Id',con.id);
}
static testMethod void TestMethod1(){
ContactUs_Enquiry__c con = new ContactUs_Enquiry__c();
ApexPages.StandardController sc = new ApexPages.StandardController(con);
ContactUsThankYouController obj = new ContactUsThankYouController(sc);
con.Prod_Mobile__c = '95473732';
con.Prod_Email__c = 'test@gmail.com';
con.Cust_Email__c = 'test';
obj.DisplayErrorMsg();
obj.checkEmail();
}
}
Thanks,
Anish
I tried test class for Error (Both DisplayErrorMsg and Checkmail) method but it didn't work.
Apex class
public void DisplayErrorMsg(){
String phone = con.Prod_Mobile__c;
system.debug('@@phone'+phone);
if(phone.length() == 8 && (phone.startsWith('6') || phone.startsWith('8') || phone.startsWith('9'))){
system.debug('if loop');
//ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Invalid Contact No. Please enter a valid mobile no (8 digits, starts with 6, 8 or 9)'));
}
else{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Check your phoine'));
system.debug('else loop');
}
}
public void checkEmail(){
string emailVal = con.Prod_Email__c;
system.debug('@@email'+emailVal);
if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}[.]{0,1}[a-zA-Z]{0,2}', emailVal))
{
EmailerrorMessage = 'Invalid email address entered.';
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Check your email'));
}
}
}
test class :
@isTest
public class ContactUsThankYouControllerTest {
static testMethod void TestMethod2()
{
ContactUs_Enquiry__c con = new ContactUs_Enquiry__c();
insert con;
con = [select Id,Name from ContactUs_Enquiry__c where id = : con.id];
PageReference pageRef = Page.ContactUsMain;
Test.setCurrentPage(pageRef);
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(con);
ApexPages.currentPage().getParameters().put('Id',con.id);
}
static testMethod void TestMethod1(){
ContactUs_Enquiry__c con = new ContactUs_Enquiry__c();
ApexPages.StandardController sc = new ApexPages.StandardController(con);
ContactUsThankYouController obj = new ContactUsThankYouController(sc);
con.Prod_Mobile__c = '95473732';
con.Prod_Email__c = 'test@gmail.com';
con.Cust_Email__c = 'test';
obj.DisplayErrorMsg();
obj.checkEmail();
}
}
Thanks,
Anish
Can you highlight which lines of your class are uncovered in the test class? Thanks