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
Naveen KvpNaveen Kvp 

how to test apex pagemessages??

Hi here i am trying to improve code coverege for test class for ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter atleast One Quantity to Create Branding Visit and Branding Order')); but i got a exception can you please help me..i post class and test class below..

class:public class Brandingcls
{
   public Branding_Visit__c brvisit{get;set;}
    public Branding_Order__c brorder{get;set;}
    public list<Branding_Item_Master__c> bimaster{get;set;}
    public list<Branding_Line_Item__c> brlineitem{get;set;}
    public Brandingcls()
    {
        bimaster=[select id,Name,Remarks__c,Quantity__c from Branding_Item_Master__c];
          brorder=new Branding_Order__c(Order_Date__c=system.Today());
        brvisit=new Branding_Visit__c(); 
    }
       
    public PageReference save()
    {
   
      list<Branding_Item_Master__c> lstbritem =new  list<Branding_Item_Master__c>();
          for(Branding_Item_Master__c bim :bimaster)
            {
                   if(bim.Quantity__c>0)
                      {
                        lstbritem.add(bim);
                      } 
            }
        if(lstbritem.Size()>0)
          {
        insert brorder;
        brvisit.Account__c = brorder.Account__c;
        brvisit.Visit_Date__c = brorder.Order_Date__c;
        insert brvisit;
        list<Branding_Line_Item__c> lstbli = new list<Branding_Line_Item__c>();     
          for(Branding_Item_Master__c bm:lstbritem)
             {
                  Branding_Line_Item__c brandli= new Branding_Line_Item__c();
                  
                   
                      brandli.Branding_Order__c=brorder.id;
                      brandli.Branding_Type__c=bm.id;
                      brandli.Quantity__c=bm.Quantity__c;
                      brandli.Remarks__c=bm.Remarks__c;
                      lstbli.add( brandli);
                
                  
             }
             if(lstbli.Size()>0)
             {
                   insert lstbli;
              }
          }
          else
          {
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter atleast One Quantity to Create Branding Visit and Branding Order'));
              return Null;
          }
             pagereference ref=new pagereference( '/' +brvisit.id); 
                ref.setredirect(true);
                   return ref;
 
     
    }
   
   
    public PageReference cancel()
    {
       return null;
    }
}


Testclass:
@istest
public class testBrandingcls{
   static testmethod  void  testsave(){
     Account Acc = new Account(Name='test',Primary_Contact_Person_Name__c='testperson', Products__c='product1',Market__c='test',Classification__c='D',Purchase_Cycle__c='test', Status__c='coldcall',Contact_Number_Mobile__c='9849544286', State__c='assam',Display_of_our_product__c='promonent',Media__c='inshop',Type_of_Dealer__c='sanitary');
  insert acc;
  Branding_Item_Master__c bim=new Branding_Item_Master__c(Quantity__c=12);
 
        insert bim;
     Branding_Order__c br=new Branding_Order__c(Order_Date__c=system.today(),Account__c=acc.id);
        insert br;
     Branding_Visit__c bv=new Branding_Visit__c(Visit_Date__c=system.today(),Account__c=acc.id);
        insert bv;
     Branding_Line_Item__c blm=new Branding_Line_Item__c(Branding_Type__c=bim.id,Branding_Order__c=br.id,Quantity__c=12);
        insert blm;
     Brandingcls obj= new Brandingcls();
          obj.save();
          obj.cancel();
  PageReference p=Page.Brandingvfpage;
    
         List<Apexpages.Message> msgs = ApexPages.getMessages();
boolean b = false;
for(Apexpages.Message msg:msgs){
    if (msg.getDetail().contains('Please Enter atleast One Quantity to Create Branding Visit and Branding Order')) b = true;
}
 
   System.assertEquals(true, ApexPages.hasMessages());
 
   }
   
}


i got 94%coverege for this class but only 2 lines belongs to apexpages is not coverd so guys please concentrate on that thing only....
          
Gaurav NirwalGaurav Nirwal
Try this command
List<Apexpages.Message> msgs = ApexPages.getMessages();
boolean b = false;
for(Apexpages.Message msg:msgs){
    if (msg.getDetail().contains('Search requires more characters')) b = true;
}
system.assert(b);