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 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
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();
        

    }
    

}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
just set   re.reg.Mail_Id__c='';
B TulasiB Tulasi
Hi Amit,
Thanks for ur reply.
Before that, I tried that thing. But That was not working. After that I have written in the above code.
B TulasiB Tulasi
Controller :

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();
        

    } */

}
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();
        

    }
    

}

 
This was selected as the best answer