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
AnshiiAnshii 

pagination with testclass

Hi Everyone, below is the code am able to cover 91 % of codecoverage unable to cover the return statements in test class.plz helpme out

Thanks in Advance.
paginationclass:
public class Diplayaccrelatecon {
    private integer totalRecs = 0;      
    private integer index = 0;
    private integer blockSize = 5;
   
   // public boolean prev {get; set;}
    //public boolean nxt {get;set;}
    //public boolean frt {get;set;}
    //public boolean sec{get;set;}
   
    //public List<Account> accList{get;set;}
   public list<Contact> conList{get;set;}
   public String accId{get;set;}
   
  public Diplayaccrelatecon() {
        totalRecs = [select count() from Account];        
    }        
    public List<Account> getacc() {//get will take the value from apex to vf
        List<Account> accs = Database.Query('SELECT id,name,phone FROM Account LIMIT :blockSize');//queried dynamically as string
        System.debug('Values are ' +accs);
        return accs;
    }          
    public void first() {
        index = index - blockSize;
        getacc();
    }     
    public void second() {
        index = index + blockSize;
       getacc();
    }
    public void save(){
        update conList;
    }
    public boolean getdisablefrt() {
        if(index == 0)
            return true;
        else
            return false;
    }      
    public boolean getdisablesec() {
        if((index + blockSize) > totalRecs)
        return true;
        else
        return false;
    }       
       public PageReference dispalyContact() {
       system.debug('accId'+accId);        
         if(accId != null)             
         if(index>=0)      
  {
      conList=[SELECT id,Name,FirstName,LastName,email FROM Contact WHERE AccountId=:accId LIMIT:blockSize ];
  }          system.debug('conList'+conList);   
             return null;       
 }     
        
    public void previous() {
        index = blockSize - index;
        dispalyContact();
    }     
    public void next() {
        index = index + blockSize;
         dispalyContact();
    }            
             
    public boolean getdisableprev() {
        if(index == 0)
            return true;
        else
            return false;
    }       
    public boolean getdisablenxt() {
        if((index + blockSize) > totalRecs)
        return true;
        else
        return false;
    }     
}
Testclass:
@isTest
public class DisplayaccrelTestclass {
   
         
@isTest
    static void call(){
         integer totalRecs = 0;     
     integer index = 0;
     integer blockSize = 5;    
        Account a=new Account();
        a.name='testacc';
        a.phone='testphone';
        insert a;
        contact c=new contact();
        c.firstName='testfirstName';
        c.LastName='testLastName';
        c.Email='test@gmail.com';
        c.AccountId=a.id;
        insert c;
        system.debug('1:'+c);
         
        Test.startTest();
        
         Diplayaccrelatecon d = new Diplayaccrelatecon ();
        //d.index=0;
        d.first();
        d.second();
       
        d.getdisablefrt();
        d.getdisablesec();
       d.next();
        d.previous();
        
       
        d.getacc();    
        c.AccountId=a.id;
        system.debug('2:'+a.id);
        contact con=[select id,Accountid from contact];
        
        system.assertEquals(con.Accountid, a.id);
       d.accId=a.id;
       d.dispalyContact();
         d.save();
       // d.getdisableprev();
        d.getdisablenxt();
        Test.stopTest();
    }
    @isTest 
    static void show(){
        integer totalRecs = 15;     
     integer index = 1;
     integer blockSize = 5; 
        Test.startTest();
        Diplayaccrelatecon ds=new Diplayaccrelatecon();
        //system.assertEquals(false, ds.getdisablefrt());
        ds.getdisablefrt();
        //system.assertEquals(false, ds.getdisablesec());
       // ds.index=5;
        ds.getdisablesec();
        ds.getdisableprev();
        ds.getdisablenxt();
        Test.stopTest();
    }
}
Suraj Tripathi 47Suraj Tripathi 47

Please find the test class of your code.

Please make the index public so that you can assign value in the test class.

public integer index = 0;
global class Diplayaccrelatecon {
    
    private integer totalRecs = 0;      
    public integer index = 0;
    private integer blockSize = 5;
   
   // public boolean prev {get; set;}
    //public boolean nxt {get;set;}
    //public boolean frt {get;set;}
    //public boolean sec{get;set;}
   
    //public List<Account> accList{get;set;}
   public list<Contact> conList{get;set;}
   public String accId{get;set;}
   
  public Diplayaccrelatecon () {
              totalRecs = [select count() from Account];    

     }        
    public List<Account> getacc() {//get will take the value from apex to vf
        List<Account> accs = Database.Query('SELECT id,name,phone FROM Account LIMIT :blockSize');//queried dynamically as string
        System.debug('Values are ' +accs);
        return accs;
    }          
    public void first() {
        index = index - blockSize;
        getacc();
    }     
    public void second() {
        index = index + blockSize;
       getacc();
    }
    public void save(){
        update conList;
    }
    public boolean getdisablefrt() {
        if(index == 0)
            return true;
        else
            return false;
    }      
    public boolean getdisablesec() {
        if((index + blockSize) > totalRecs)
        return true;
        else
        return false;
    }       
       public PageReference dispalyContact() {
       system.debug('accId'+accId);        
         if(accId != null) 
             System.debug('Index Inside display Contact:::'+index);
         if(index>=0)      
  {
      System.debug('inside dispalyContact index:::::'+index);
      System.debug('inside dispalyContact blockSize:::::'+blockSize);
      conList=[SELECT id,Name,FirstName,LastName,email FROM Contact where AccountId=:accId   LIMIT :blockSize];
  }          system.debug('conList::::::::::::::::::::::'+conList);   
             return null;       
 }     
        
    public void previous() {
        index = blockSize - index;
        dispalyContact();
    }     
    public void next() {
        index = index + blockSize;
         dispalyContact();
    }            
             
    public boolean getdisableprev() {
        if(index == 0)
            return true;
        else
            return false;
    }       
    public boolean getdisablenxt() {
        if((index + blockSize) > totalRecs)
        return true;
        else
        return false;
    }     

     
}

 

@isTest
public class DisplayaccrelTestclass {
    
    @isTest
    static void call(){
        integer totalRecs = 0;     
        integer index = 0;
        integer blockSize = 5;    
        Account a=new Account();
        a.name='testacc';
        a.phone='testphone';
        insert a;
        contact c=new contact();
        c.firstName='testfirstName';
        c.LastName='testLastName';
        c.Email='test@gmail.com';
        c.AccountId=a.id;
        insert c;
        system.debug('1:'+c);
        
        Test.startTest();
        
        Diplayaccrelatecon d = new Diplayaccrelatecon();
        d.index=20;
        d.first();
        d.second();
        
        d.getdisablefrt();
        d.getdisablesec();
        d.next();
        d.previous();
        
        
        d.getacc();    
        c.AccountId=a.id;
        system.debug('2:'+a.id);
        contact con=[select id,Accountid from contact];
        
        system.assertEquals(con.Accountid, a.id);
        d.accId=a.id;
        d.dispalyContact();
        d.save();
        // d.getdisableprev();
        d.getdisablenxt();
        Test.stopTest();
    }
    @isTest 
    static void show(){
        integer totalRecs = 15;     
        integer index = 1;
        integer blockSize = 5; 
        Test.startTest();
        Diplayaccrelatecon ds=new Diplayaccrelatecon();
        //system.assertEquals(false, ds.getdisablefrt());
        ds.getdisablefrt();
        //system.assertEquals(false, ds.getdisablesec());
        ds.index=30;
        ds.getdisablesec();
        ds.getdisableprev();
        ds.getdisablenxt();
        Test.stopTest();
    }
}

Please let me know it is working or not??

Please mark it as the Best Answer if it helps you.

Thank You

Suraj Tripathi 47Suraj Tripathi 47
It is working or not?
AnshiiAnshii
No suraj , I did it by making the changes in show method .. Thanks.