+ Start a Discussion
amruta_dhumalamruta_dhumal 

Need help in writing test class for controller

Hi There,

 

I'm only getting 42% code coverage with error msg :

 

System.NullPointerException: Attempt to de-reference a null object.

Class.PrewiredSearch.runSearch: line 282, column 1 Class.PrewiredSearch.Beginning: line 472, column 1 Class.testSearch.testSearch: line 71, column 1

 

Any advise?

 

Class:

 

public with sharing class PrewiredSearch{
private ApexPages.StandardController controller;
public String retURL {get; set;}
public String saveNewURL {get; set;}
public String rType {get; set;}
public String cancelURL {get; set;}
public String ent {get; set;}
public String confirmationToken {get; set;}
String recordId;
String oppName;
public String accntName {get; set;}

 private integer counter=0;  //keeps track of the offset
   private integer list_size=20; //sets the page size or number of rows
   public integer total_size; //used to show user the total size of the list

String country1 = null;
Public String selection = '';
public Boolean shouldRender= false;
  private String soqlWhereClause {get;set;}

public String country {get; set;}

  public List<Site_Address__c> sites{get;set;}
  public Site_Address__c site{get;set;}

  Public String province {get; set;}
  Public String city {get; set;}
  Public String street {get; set;}
  Public String suburb {get; set;}
   Public Boolean flagNoRecordFound  {get; set;}
   Public Boolean flagNoApplicable {get; set;}


 public PrewiredSearch(ApexPages.StandardController  controller) {
 ///this.controller = controller;
  this.site= (Site_Address__c)controller.getRecord();

 recordId=ApexPages.currentPage().getParameters().get('Id');
 oppName=ApexPages.currentPage().getParameters().get('Name');
retURL = ApexPages.currentPage().getParameters().get('retURL');
rType = ApexPages.currentPage().getParameters().get('RecordType');
 cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');
ent = ApexPages.currentPage().getParameters().get('ent');
confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');
saveNewURL = ApexPages.currentPage().getParameters().get('save_new_url');
accntName = ApexPages.currentPage().getParameters().get('CF00N20000002wMtX'); //lkid
system.debug('accntName :'+accntName );
       soql = 'select Country_Name__c, id,Name,Province__c, Pre_Wired_Site_Name__c,Pre_Wired_Site_Name__r.Name,City_Name__c,ignoredhidden__c,Ignored_Records__c, Street_Name__c,Suburb_Name__c from Site_Address__c where Establishment_Type__c in(\'Pre-Ducted Building\',\'Pre-Wired Building\')';
       soql += ' and Country_Name__c LIKE \'%South Africa%\'';       
       soqlWhereClause =' Country_Name__c LIKE \'%South Africa%\'';       
       runQuery();

 flagNoRecordFound=false;
flagNoApplicable =true;
    }
   public boolean getShouldRender() {
return shouldRender;
}

public void setBoolean(Boolean shouldRender) {
this.shouldRender= shouldRender;
}

 public boolean getFlagNoRecordFound() {

return this.flagNoRecordFound;
}

public void setFlagNoRecordFound(Boolean flagNoRecordFound) {
this.flagNoRecordFound= flagNoRecordFound;
}

 public boolean getFlagNoApplicable() {
return this.flagNoApplicable;
}

public void setFlagNoApplicable(Boolean flagNoApplicable) {
this.flagNoApplicable= flagNoApplicable;
}

 public String getString() {
return selection ;
}

public void setString(String selection ) {
this.selection = selection ;
}

public void Disable() {

if (selection.equals('Select')) {
shouldRender= true;
} else {
shouldRender= false;
}
}

PageReference pageRef ;
    private String soql {get;set;}
 

public PageReference redirect(){
PageReference pageRef ;
PageReference returnURL ;
 if(rType=='01220000000JJaz') {
 
            if ((accntName==null)){ 
           
 
              returnURL = new PageReference('/apex/PrewiredSearchVFPage');
             
              }
           else      
           {
             returnURL = new PageReference('/apex/PrewiredSearchVFPage?CF00N20000002wMtX='+accntName+'');

             }
            returnURL.setRedirect(true);
      }      
      return returnURL;
      If(rType=='012P00000000Hcj'){
  returnURL = new PageReference('/a0v/e?retURL=%2Fa0vP0000000xABk');
   returnURL.setRedirect(true);
  
  }return returnURL;
 if(rType=='012P00000000HrW'){
  returnURL = new PageReference('/a0v/e?retURL=%2Fa0vP0000000xABk');
   returnURL.setRedirect(true);
  
  }return returnURL;
  if(rType=='01220000000JJb4'){
  returnURL = new PageReference('/a0v/e?retURL=%2Fa0vP0000000xABk');
   returnURL.setRedirect(true);
  
  }
  return returnURL;
  }
   public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }
  public String sortField {
    get  { if (sortField == null) {sortField = 'Pre_Wired_Site_Name__c'; } return sortField;  }
    set;
  }
   public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
    set;
  }
    public PrewiredSearch() {
   
  }
   public void toggleSort() {
        sortDir = sortDir.equals // run the query again
    runQuery();
  }
 List<Site_Address__c> totalSiteAddr{get;set;}

     public void runQuery() {
     try {
    String soqlTot='select id from Site_Address__c where '+ soqlWhereClause ;
     System.debug('soqlTot:'+ soqlTot);

    totalSiteAddr =Database.query(soqlTot); //[select id  from Site_Address__c where  soqlWhereClause];
     total_size=totalSiteAddr.size();
     sites= Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit :list_size  offset :counter');
               System.debug('sites :'+sites.size()+' ::counter :'+counter +':: total_size :'+total_size);
                         If (sites.isEmpty())
         {
         flagNoRecordFound=true;
           flagNoApplicable=false;
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Pre-Wired Name not found  or the search does not contain any name,Please select No Records Found Section'));
   
        }
        else
           {
              flagNoRecordFound=false;
              flagNoApplicable=true;
        }
   } catch (NullPointerException npe) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Pre-Wired Name not found  or the search does not contain any name'));
    }
   
    catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops! Please try your search again'));
    }
 
 }
     public PageReference runSearch() {
  
     
     String suburb= Apexpages.currentPage().getParameters().get('suburb');
    String street= Apexpages.currentPage().getParameters().get('street');
    
    soql = 'select Country_Name__c, id,Name,Province__c, Pre_Wired_Site_Name__c,Pre_Wired_Site_Name__r.Name,City_Name__c,ignoredhidden__c,Ignored_Records__c, Street_Name__c,Suburb_Name__c from Site_Address__c where Establishment_Type__c in(\'Pre-Ducted Building\',\'Pre-Wired Building\')';
   
    if (site.Country_Name__c!=null) 
    { 
      if(site.Country_Name__c.length() > 0)
      {
           soql += ' and Country_Name__c LIKE \'%'+ String.escapeSingleQuotes(site.Country_Name__c)+'%\'';
           }
       
       if (site.Province__c!=null ) 
        if(site.Province__c.length() > 0) 
        {
           soql += ' and Province__c LIKE \'%'+ String.escapeSingleQuotes(site.Province__c)+'%\'';
         
          soqlWhereClause  +=' and Province__c LIKE \'%'+ String.escapeSingleQuotes(site.Province__c)+'%\'';


                   }
          
       if (site.City_Name__c!=null)
         if(site.City_Name__c.length() > 0)    {
           soql += ' and City_Name__c LIKE \'%'+String.escapeSingleQuotes(site.City_Name__c)+'%\'';
            soqlWhereClause  += ' and City_Name__c LIKE \'%'+String.escapeSingleQuotes(site.City_Name__c)+'%\'';

          
          
           }
          
        if (street!=null)   
         if(street.length() > 0)  
         {
           soql += ' and Street_Name__c LIKE \'%'+String.escapeSingleQuotes(street)+'%\'';
             soqlWhereClause  += ' and Street_Name__c LIKE \'%'+String.escapeSingleQuotes(street)+'%\'';

           }
          
        if (suburb!=null)  
           if(suburb.length() > 0) 
           {  
           soql += ' and Suburb_Name__c LIKE \'%'+String.escapeSingleQuotes(suburb)+'%\'';
            soqlWhereClause  += ' and Suburb_Name__c LIKE \'%'+String.escapeSingleQuotes(suburb)+'%\'';

          
           }


       runQuery();
   }
  else
  {
       soql += ' and Country_Name__c LIKE \'%'+String.escapeSingleQuotes('null')+'%\''; 
  
   runQuery();
  }
     return null;
  }
 

 public PageReference Clickhere(){
if(accntName==null)
  pageRef = new PageReference('/a0v/e?retURL=%2Fa0vP0000000xABk&00NW0000000ILAj=Yes&00NW0000000ILAe=Yes&00N20000002wMth='+site.Country_Name__c+'&00N20000002wMtc='+site.City_Name__c+'&00N20000002wMu1='+site.Province__c+'');
else
   pageRef = new PageReference('/a0v/e?retURL=%2Fa0vP0000000xABk&00NW0000000ILAj=Yes&00NW0000000ILAe=Yes&00N20000002wMth='+site.Country_Name__c+'&00N20000002wMtc='+site.City_Name__c+'&00N20000002wMu1='+site.Province__c +'&CF00N20000002wMtX='+accntName);
  
    PageRef.setRedirect(true);
   PageRef.getParameters().put('nooverride', '1');
       

Return PageRef;
  }
public PageReference Clickhere1(){
       if(accntName==null)
          pageRef = new PageReference('/a0v/e?retURL=%2Fa0vP0000000xABk&00NW0000000ILAj=Yes&00N20000002wMth='+site.Country_Name__c+'&00N20000002wMtc='+site.City_Name__c+'&00N20000002wMu1='+site.Province__c );
     else    
       pageRef = new PageReference('/a0v/e?retURL=%2Fa0vP0000000xABk&00NW0000000ILAj=Yes&00N20000002wMth='+site.Country_Name__c+'&00N20000002wMtc='+site.City_Name__c+'&00N20000002wMu1='+site.Province__c +'&CF00N20000002wMtX='+accntName);

   PageRef.setRedirect(true);
   PageRef.getParameters().put('nooverride', '1');
       

Return PageRef;
  }
 public PageReference cancel() {
          PageReference pr = new PageReference('a0v/o');
          pr.setRedirect(true);
          return pr;
     }
    
 public PageReference test() {
 
      String siteId= Apexpages.currentPage().getParameters().get('siteId');
         return null;
    }
    public PageReference runSearch2() {
 PageReference pageRef ;
     String siteId= Apexpages.currentPage().getParameters().get('siteId');
      String street= Apexpages.currentPage().getParameters().get('street');
       String suburb= Apexpages.currentPage().getParameters().get('suburb');
     List<Site_Address__c> site = [select Country_Name__c,Name, id,Province__c, Pre_Wired_Site_Name__c,Pre_Wired_Site_Name__r.Name,City_Name__c,ignoredhidden__c,Ignored_Records__c, Street_Name__c,Suburb_Name__c from Site_Address__c where id=:siteId ];
        for(Site_Address__c siteObj : site )    
             { 
             
                 if ((suburb!='') && (street=='')){
         system.debug('inside runSearch2');
          if(accntName==null)
             pageRef = new PageReference('/a0v/e?&retURL=%2Fa0vP0000000xABk&00N20000002wMth='+siteObj.Country_Name__c+'&00N20000002wMtc='+siteObj.City_Name__c+'&CF000N20000002xCO6='+siteObj.Name+'&00NP0000000fkqZ='+siteObj.Pre_Wired_Site_Name__r.Name+'&00N20000002wMu1='+siteObj.Province__c+'&00N20000002wMuL='+suburb+'&00NW0000000ILAj=No');
          else
             pageRef = new PageReference('/a0v/e?&retURL=%2Fa0vP0000000xABk&00N20000002wMth='+siteObj.Country_Name__c+'&00N20000002wMtc='+siteObj.City_Name__c+'&CF00N20000002xCO6='+siteObj.Name+'&CF00NP0000000fkqZ='+siteObj.Pre_Wired_Site_Name__r.Name+'&00N20000002wMu1='+siteObj.Province__c+'&00N20000002wMuL='+suburb+'&CF00N20000002wMtX='+accntName+'&00NW0000000ILAj=No');
       
         PageRef.setRedirect(true);
         PageRef.getParameters().put('nooverride', '1');
   }
   else if ((suburb=='') && (street!='')){
         system.debug('inside runSearch2');
          if(accntName==null)
             pageRef = new PageReference('/a0v/e?&retURL=%2Fa0vP0000000xABk&00N20000002wMth='+siteObj.Country_Name__c+'&00N20000002wMtc='+siteObj.City_Name__c+'&CF00N20000002xCO6='+siteObj.Name+'&00NP0000000fkqZ='+siteObj.Pre_Wired_Site_Name__r.Name+'&00N20000002wMu1='+siteObj.Province__c+'&00N20000002wMuB='+street+'&00NW0000000ILAj=No');       
           else
             pageRef = new PageReference('/a0v/e?&retURL=%2Fa0vP0000000xABk&00N20000002wMth='+siteObj.Country_Name__c+'&00N20000002wMtc='+siteObj.City_Name__c+'&CF00N20000002xCO6='+siteObj.Name+'&00NP0000000fkqZ='+siteObj.Pre_Wired_Site_Name__r.Name+'&00N20000002wMu1='+siteObj.Province__c+'&00N20000002wMuB='+street+'&CF00N20000002wMtX='+accntName+'&00NW0000000ILAj=No');

         PageRef.setRedirect(true);
         PageRef.getParameters().put('nooverride', '1');
   }
   else if ((suburb!='') && (street!='')){
         system.debug('inside runSearch2');
            if(accntName==null)
             pageRef = new PageReference('/a0v/e?&retURL=%2Fa0vP0000000xABk&00N20000002wMth='+siteObj.Country_Name__c+'&00N20000002wMtc='+siteObj.City_Name__c+'&CF00N20000002xCO6='+siteObj.Name+'&00NP0000000fkqZ='+siteObj.Pre_Wired_Site_Name__r.Name+'&00N20000002wMu1='+siteObj.Province__c+'&00N20000002wMuB='+street+'&00N20000002wMuL='+suburb+'&00NW0000000ILAj=No');
        else
         pageRef = new PageReference('/a0v/e?&retURL=%2Fa0vP0000000xABk&00N20000002wMth='+siteObj.Country_Name__c+'&00N20000002wMtc='+siteObj.City_Name__c+'&CF00N20000002xCO6='+siteObj.Name+'&00NP0000000fkqZ='+siteObj.Pre_Wired_Site_Name__r.Name+'&00N20000002wMu1='+siteObj.Province__c+'&00N20000002wMuB='+street+'&00N20000002wMuL='+suburb+'&CF00N20000002wMtX='+accntName+'&00NW0000000ILAj=No');
       
         PageRef.setRedirect(true);
         PageRef.getParameters().put('nooverride', '1');
   }
    else{        
         if(accntName==null)
                pageRef = new PageReference('/a0v/e?&retURL=%2Fa0vP0000000xABk&00N20000002wMth='+siteObj.Country_Name__c+'&00N20000002wMtc='+siteObj.City_Name__c+'&CF00N20000002xCO6='+siteObj.Name+'&00NP0000000fkqZ='+siteObj.Pre_Wired_Site_Name__r.Name+'&00N20000002wMu1='+siteObj.Province__c+'&00NW0000000ILAj=No');
          else
          pageRef = new PageReference('/a0v/e?&retURL=%2Fa0vP0000000xABk&00N20000002wMth='+siteObj.Country_Name__c+'&00N20000002wMtc='+siteObj.City_Name__c+'&CF00N20000002xCO6='+siteObj.Name+'&00NP0000000fkqZ='+siteObj.Pre_Wired_Site_Name__r.Name+'&00N20000002wMu1='+siteObj.Province__c+'&CF00N20000002wMtX='+accntName+'&00NW0000000ILAj=No');

         
        PageRef.setRedirect(true);
   PageRef.getParameters().put('nooverride', '1');
        }
                                    
        }
        return pageRef ;

        }

        public void StatesSelectList() {
String country= Apexpages.currentPage().getParameters().get('country');
  }
       public PageReference Beginning() { //user clicked beginning
      counter = 0;
      runSearch();
      return null;
   }

   public PageReference Previous() { //user clicked previous button
      counter -= list_size;
            runSearch();
      return null;
   }

   public PageReference Next() { //user clicked next button
      counter += list_size;
            runSearch();
      return null;
   }

   public PageReference End() { //user clicked end
      counter = total_size - math.mod(total_size, list_size);
            runSearch();
      return null;
   }

   public Boolean getDisablePrevious() { 
           if (counter>0) return false; else return true;
   }

   public Boolean getDisableNext() { //this will disable the next and end buttons
      if (counter + list_size < total_size) return false; else return true;
   }

   public Integer getTotal_size() {
   System.debug('total_size:'+total_size);
      return total_size;
   }

   public Integer getPageNumber() {
      return counter/list_size + 1;
   }

   public Integer getTotalPages() {
   System.debug('total_size:'+total_size  +':: list_size :'+list_size );

      if (math.mod(total_size, list_size) > 0) {
         return total_size/list_size + 1;
      } else {
         return (total_size/list_size);
      }
   }

   }

 

 

 

 

Amruta

 

amruta_dhumalamruta_dhumal

here is my test class:

 

@isTest
private class testSearch {
static testMethod void testSearch()
{
Profile p = [select id from profile where name='Standard User'];

User u = new User(alias = 'test123', email='test123@noemail.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, country='United States',
timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
insert u;
Account acc=new Account(Name='TEST_DEV_WARRICK',Neotel_Customer_Type__c='Gauteng Mid Market',OwnerId = u.Id,Account_Checked_on_DEG__c=true,RecordTypeId='01220000000LXrL');
insert acc;

 

Site_Address__c si = new Site_Address__c(
Name='Test123',Account_Name__c=acc.id,Establishment_Type__c='Farm',City_Name__c='Sandton',Country_Name__c='South Africa',Street_Name__c='Fricker Road',
Suburb_Name__c='Sandton',Province__c='GAUTENG');

insert si;

PageReference pg = Page.PrewiredSearchVFPage;
Test.setCurrentPage(pg);
pg.getParameters().put('id', 'si.Account_Name__c');

 

// instantiate the controller
PrewiredSearch  controller=new PrewiredSearch ();

System.assert(controller.runSearch2()==null);
//System.assert(controller.runSearch() == null);

 

String testDebugSoql = controller.debugSoql;

controller.toggleSort();
System.assertequals (controller.sortDir, 'desc');
controller.getShouldRender();
controller.getString();
controller.getflagNoRecordFound();
controller.getflagNoApplicable();
//controller.getProfiles();

String sText ='Yes';
controller.setString(sText);

Boolean sText3 =true;
controller.setBoolean(sText3);

Boolean sText4 =true;
controller.setFlagNoRecordFound(sText4);

Boolean sText5 =true;
controller.setFlagNoApplicable(sText5);

 

controller.Disable();
controller.redirect();
controller.cancel();
controller.StatesSelectList();
controller.test();

//to test the catch clause we need to make the query fail
//I think that this will do it but I am not sure
controller.sortField = 'Some invalid sort field';
controller.runQuery();
controller.Beginning();

controller.Previous();

controller.Next();
controller.End();
controller.getTotal_size();
controller.getDisablePrevious();
controller.getDisableNext();
controller.getPageNumber();
controller.getTotalPages();

controller.Clickhere1();
controller.Clickhere();
}


}

 

Amruta