+ Start a Discussion
Force.platformForce.platform 

write test class for custom controller

Hello All,
      can anyone provide test class for following controller.
public class Account_Pagination_With_Notes {
    public List<Account> accList{get; set;}
    public List<Account_Notes__c> noteList{get; set;}
    public List<WrapperClass> wrapperList{get; set;} 
    
    public integer totalRecs = 0;
    public integer OffsetSize = 0;
    public integer LimitSize= 10;
    
    public Account_Pagination_With_Notes(){ 
     totalRecs =[select count() from account];
     system.debug('Total Account Records'+totalRecs);
     PaginationAccountNote();
     
    }
    
    public void FirstPage()
    {
        OffsetSize = 0;
        system.debug('offset on first page'+OffsetSize);
        updateAccountNote();
        PaginationAccountNote();
    }
    
    public void previous()
    {
        OffsetSize = OffsetSize - LimitSize;
        system.debug('offset on previous page'+OffsetSize);
        updateAccountNote();
        PaginationAccountNote();
    }
    
    public void next()
    {
        OffsetSize = OffsetSize + LimitSize;
        system.debug('offset on next page'+OffsetSize);
        updateAccountNote();
        PaginationAccountNote();
    }
    
    public void LastPage()
    {
        OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
        system.debug('offset on last page'+OffsetSize);
        updateAccountNote();
        PaginationAccountNote();
    }
    
    public boolean getprev()
    {
        if(OffsetSize == 0)
            return true;
        else
            return false;
    }
    
    public boolean getnxt()
    {
        if((OffsetSize + LimitSize) > totalRecs)
            return true;
        else
            return false;
    }
    
    public void PaginationAccountNote(){
        accList=[SELECT id, name, (SELECT id, name, Note__c from Account_Notes__r ORDER BY CreatedDate DESC LIMIT 1) from Account  ORDER BY NAME ASC LIMIT :LimitSize OFFSET :OffsetSize];
        
        wrapperList= new List<WrapperClass>();
      
        for(Account acc : accList){
            Account_Notes__c nte = new Account_Notes__c();
            
            if(acc.Account_Notes__r.size()>0){
                nte = acc.Account_Notes__r[0];
            }else{
             nte.Account__c = acc.Id;
         }
            
            wrapperList.add(new WrapperClass(acc,nte));
            system.debug('--------------'+wrapperList);
            
        }       
    }
    
    public void updateAccountNote(){
         list<Account_Notes__c> addNotes = new list<Account_Notes__c>();
         for(WrapperClass wrap : wrapperList){
            if(!String.isblank(wrap.noteRec.Note__c) || wrap.noteRec.Id != null){
                addNotes.add(wrap.noteRec);
            }   
         }
        
        upsert addNotes;
    }
    
    public class WrapperClass{
        public Account accRec {get; set;}
        public Account_Notes__c noteRec {get; set;}
        public WrapperClass(Account acc , Account_Notes__c note)
        {
            accRec = acc ;
            noteRec = note;
        }
        
    }
}
sfdcMonkey.comsfdcMonkey.com
hi use below test class it will give you move then 80% code coverge :
@isTest
private class testClass
{
    static testMethod void TestLead()
    { 
	  account acc = new account() ;
        acc.Name = 'test';
        insert acc;
        
        Account_Notes__c an = new Account_Notes__c();
        an.Account__c = acc.Id;
        an.Name = 'test';
        an.Note__c = 'test 123';
        
        insert an;
        
        Account_Pagination_With_Notes apwn = new Account_Pagination_With_Notes();
        apwn.next();
        apwn.FirstPage();
        apwn.LastPage();
        apwn.getprev();
        apwn.getnxt();
    }
}
i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks
 sfdcmonkey.com