+ Start a Discussion
ForceLoverForceLover 

can somebody help to write test class for my VF page it is just covering only 19%

I have a VF page which will search for records based on a string value REFID here is my controller please help me in this regard

 

public  class SiteLead {
    
    private ApexPages.StandardController controller {get; set;}
    public List<LeadBackup__c> searchResults {get;set;}
     public List<LeadBackup__c> searchResults1 {get;set;}
    public string searchText {get;set;}
    //public Lead Leads;
   
    
    public SiteLead(ApexPages.StandardController controller) {
    


    }
    
    List<Lead> Leads=[select id, name, FirstName, LastName, Status, Ref_ID_File_Number__c, Email, Phone, Street, City, State, Country, PostalCode, TAB_Message__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,DoNotCall from Lead where Ref_ID_File_Number__c =:searchText AND IsConverted=false ];
    
    public PageReference search() {
   // String qry = 'select id,Ref_ID_File_Number__c, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,DoNotCall__c from LeadBackup__c' + where Ref_ID_File_Number__c = searchText  order by name';
    searchResults1 = [select id,RefID_FileNumber__c,Name, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, TAB_Time_stamp__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,Do_Not_Call__c from LeadBackup__c where RefID_FileNumber__c =:searchText  order by name];
    //searchResults = Database.query(qry);
   
     
     //searchResults = [select id, name, FirstName, LastName, Status, Ref_ID_File_Number__c, Email, Phone, Street, City, State, Country, PostalCode, TAB_Message__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,DoNotCall from Lead where Ref_ID_File_Number__c =:searchText  order by name];

   
    if(searchResults1.size()==0){

          LeadBackup__c lb = new  LeadBackup__c(RefID_FileNumber__c = searchText, 
                                                Name=[select name,id from lead where Ref_ID_File_Number__c=:searchtext ].Name,
                                                City__c=[select name,id,city from lead where Ref_ID_File_Number__c=:searchtext ].City,
                                                Street__c=[select name,id,street from lead where Ref_ID_File_Number__c=:searchtext ].street,
                                                State__c=[select name,id,state from lead where Ref_ID_File_Number__c=:searchtext ].state,
                                                Lead__c = [select name,id from lead where Ref_ID_File_Number__c=:searchtext ].id, 
                                                Email__c = [select name,id,Email from lead where Ref_ID_File_Number__c=:searchtext ].Email, 
                                                Phone__c = [select name,id,Email,Phone from lead where Ref_ID_File_Number__c=:searchtext ].Phone,
                                                Evening_Phone__c = [select name,id,Evening_Phone__c  from lead where Ref_ID_File_Number__c=:searchtext ].Evening_Phone__c, 
                                                Preferred_Call_Time__c = [select name,id,Evening_Phone__c,Preferred_Call_Time__c   from lead where Ref_ID_File_Number__c=:searchtext ].Preferred_Call_Time__c, 
                                                Notes__c = [select name,id,Evening_Phone__c,Notes__c   from lead where Ref_ID_File_Number__c=:searchtext ].Notes__c,
                                                Do_Not_Call__c = [select name,id,Evening_Phone__c,DoNotCall   from lead where Ref_ID_File_Number__c=:searchtext ].DoNotCall);
          //searchResults.add(lb);
          system.debug('lb'+lb);
          insert lb ;
          
          searchResults = [select id,RefID_FileNumber__c,Name, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, TAB_Time_stamp__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,Do_Not_Call__c from LeadBackup__c where RefID_FileNumber__c =:searchText  order by name];

          
    }else{
    
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'<b>Please confirm name and address below. Address will be where the letter was received/addressed. If these do not match, you will need to re-verify Ref ID, as it may have been entered incorrectly</b>'));
       
       searchResults = [select id,RefID_FileNumber__c,Name, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, TAB_Time_stamp__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,Do_Not_Call__c from LeadBackup__c where RefID_FileNumber__c =:searchText  order by name];

       return null;
    }
    return null;
  }
  
  
  public PageReference save() {
   
   if(!(String.isEmpty(searchText))){
          
       try {
            
            list<LeadBackup__c> lstupdate = new list<LeadBackup__c>();
            
                 
             For(LeadBackup__c le:searchResults){
             
             //le.Id = lead.Id;
             le.RefID_FileNumber__c = searchResults[0].RefID_FileNumber__c ;
             le.Lead__c = [select name,id from lead where Ref_ID_File_Number__c=:searchtext ].id;
             le.Name = searchResults[0].Name;
             le.Email__c = searchResults[0].Email__c;
             le.Phone__c = searchResults[0].Phone__c;
             le.Evening_Phone__c = searchResults[0].Evening_Phone__c ;
             le.Preferred_Call_Time__c = searchResults[0].Preferred_Call_Time__c;
             le.Notes__c = searchResults[0].Notes__c;
             le.Do_Not_Call__c = searchResults[0].Do_Not_Call__c;
             le.TAB_Message__c = True;
             le.TAB_Time_stamp__c = System.now();
             lstupdate.add(le);
             }
             
             update lstupdate;
      
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info,'Record <b>'+searchResults[0].Name+'</b> has been Updated'));
            
    } Catch (DMLException e) {
      ApexPages.addMessages(e);
      return null;
    }
   
    searchText='';
    searchResults.clear(); 
    return null;
       
  }
  else
  {
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info,'Please enter REFID/FileNumber'));
  }
  
  return null;
 
}
  public PageReference cancel() {
       
       pageReference pg1 = new pageReference('/apex/siteupdatinglead');
       return(pg1.setredirect(true));
       
  }

}

 this is so far my test class which is covering only 19%

 

Also i'm getting an error System.Query exception on test run i showed it below

 

@isTest
private class TestsiteLead{

private static testmethod void testing(){

     PageReference pgRef = Page.siteupdatinglead;
     
     Test.setCurrentPage(pgRef);
      
     string searchtext='AS098-87658';
     
     User u1 = [select id,name from user where id=:UserInfo.getUserId()];
   
     Lead le = new Lead(Status='Mailed',  FirstName='wahed', LastName='testle',  
                       OwnerId=u1.Id, MobilePhone= '4695690396', Phone= '4695690397',Ref_ID_File_Number__c='AS098-87658',City='test city',
                       street='test street',state='test state',Evening_Phone__c='5648972315',Preferred_Call_Time__c=system.now(),
                       Notes__c ='test',DoNotCall=true );
     insert le;
    
     Leadbackup__c lb = new Leadbackup__c(Name='testbackup', Lead__c=le.id, Phone__c='9703046831',RefID_FileNumber__c='AS098-87658');
     insert lb;

     ApexPages.StandardController sc = new ApexPages.standardController(le);

     SiteLead SL = new SiteLead(sc);

     SL.search();-------Also i'm getting an error System.Query exception on test run
     
     Leadbackup__c lb1=[select id,name,phone__c from LeadBackup__c where id=:lb.id];
     lb1.Phone__c='4695690396';
     update lb1;
     
      SL.save();
      
      SL.cancel();

     
} 


}

 

ForceLoverForceLover

On my test run i'm getting this error

 

System.QueryException: List has no rows for assignment to SObject

Class.SiteLead.search: line 29, column 1
Class.TestsiteLead.testing: line 36, column 1

 

This is from where my 29 line starts in my controller that is in search method

 

public PageReference search() {
   // String qry = 'select id,Ref_ID_File_Number__c, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,DoNotCall__c from LeadBackup__c' + where Ref_ID_File_Number__c = searchText  order by name';
    searchResults1 = [select id,RefID_FileNumber__c,Name, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, TAB_Time_stamp__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,Do_Not_Call__c from LeadBackup__c where RefID_FileNumber__c =:searchText  order by name];
    //searchResults = Database.query(qry);
   
     
     //searchResults = [select id, name, FirstName, LastName, Status, Ref_ID_File_Number__c, Email, Phone, Street, City, State, Country, PostalCode, TAB_Message__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,DoNotCall from Lead where Ref_ID_File_Number__c =:searchText  order by name];

   
    if(searchResults1.size()==0){

          LeadBackup__c lb = new  LeadBackup__c(RefID_FileNumber__c = searchText,
                                                Name=[select name,id from lead where Ref_ID_File_Number__c=:searchtext ].Name,
                                                City__c=[select name,id,city from lead where Ref_ID_File_Number__c=:searchtext ].City,
                                                Street__c=[select name,id,street from lead where Ref_ID_File_Number__c=:searchtext ].street,
                                                State__c=[select name,id,state from lead where Ref_ID_File_Number__c=:searchtext ].state,
                                                Lead__c = [select name,id from lead where Ref_ID_File_Number__c=:searchtext ].id,
                                                Email__c = [select name,id,Email from lead where Ref_ID_File_Number__c=:searchtext ].Email,
                                               Phone__c = [select name,id,Email,Phone from lead where Ref_ID_File_Number__c=:searchtext ].Phone, Evening_Phone__c = [select name,id,Evening_Phone__c  from lead where Ref_ID_File_Number__c=:searchtext ].Evening_Phone__c,

 

Preferred_Call_Time__c = [select name,id,Evening_Phone__c,Preferred_Call_Time__c   from lead where Ref_ID_File_Number__c=:searchtext ].Preferred_Call_Time__c,

 

Notes__c = [select name,id,Evening_Phone__c,Notes__c   from lead where Ref_ID_File_Number__c=:searchtext ].Notes__c,
Do_Not_Call__c = [select name,id,Evening_Phone__c,DoNotCall   from lead where Ref_ID_File_Number__c=:searchtext ].DoNotCall);
          system.debug('lb'+lb);
          insert lb ;
          
          searchResults = [select id,RefID_FileNumber__c,Name, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, TAB_Time_stamp__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,Do_Not_Call__c from LeadBackup__c where RefID_FileNumber__c =:searchText  order by name];

          
    }else{
    
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'<b>Please confirm name and address below. Address will be where the letter was received/addressed. If these do not match, you will need to re-verify Ref ID, as it may have been entered incorrectly</b>'));
       
       searchResults = [select id,RefID_FileNumber__c,Name, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, TAB_Time_stamp__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,Do_Not_Call__c from LeadBackup__c where RefID_FileNumber__c =:searchText  order by name];

       return null;
    }
    return null;
  }