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
Amol Gaikwad 13Amol Gaikwad 13 

I need to cover getHasNext() method and cath part of class using apex test class

Apex Class-


public class CS_AcceptOldestCaseController {
    public String filterId {get; set;}
    public Boolean hasErrors {get; set;}
    public Case oldestCase {get; set;}
    
    // Constructor to accept oldest case
    public CS_AcceptOldestCaseController(ApexPages.StandardSetController cont){
        try {
            // get the 15 digit filter id for return information
            filterId = String.valueOf(cont.getFilterId()).substring(0, 15);
            
            // get all record ids in the list view
            List<Id> caseIds = new List<Id>();
            for(Case c : (List<Case>) cont.getRecords()){
                caseIds.add(c.Id);
            }
            while(cont.getHasNext()){
                for(Case c : (List<Case>) cont.getRecords()){
                    caseIds.add(c.Id);
                }
                cont.next();
            }
            
            // get all records in the list that have a owner of type Queue sorted by last modified date, then assign the first one to treat
            System.Debug('caseIds:' + caseIds);
            List<Case> cases = [select Id, OwnerId, CaseNumber from Case where Id=: caseIds and Owner.Type = 'Queue' order by CreatedDate ASC];
            if (cases.size() > 0){
                oldestCase = cases[0];
            } else {
                oldestCase = null;
            }
            
            hasErrors = false;
         } catch (Exception ex){
            hasErrors = true;
            ApexPages.addMessages(ex);
        }
        
    }
    
    // accept the case
    public PageReference acceptCase(){
        try {
            String successMsg = 'No queue cases found';
            if (oldestCase != null){
                oldestCase.OwnerId = UserInfo.getUserId();
                update oldestCase;
                
                successMsg = 'Case ' + oldestCase.CaseNumber + ' accepted';
            } else  {
                hasErrors = true;
            }
            
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,successMsg);
            ApexPages.addMessage(myMsg);
            
        } catch (Exception ex){
            hasErrors = true;
            ApexPages.addMessages(ex);
        }
        
        return null;
    }

}


Test Class-

@isTest
public class CS_AcceptOldestCaseControllerTest {
    
     public testmethod static void testAcceptOldestCaseController()  {
         List<Case> cases=new  List<Case>();        
          Group g = new Group(Name='TestQueue',Type='Queue');
          System.runas(new User(Id=UserInfo.getUserId())) {
              insert g;
              insert new QueueSobject(QueueId=g.id, SObjectType='Case');
          }
         
         
           Case c = new Case(Subject='test', Client_ID__c = 'test', Contact_Email__c ='test@test.com');
          insert c;
        Case c1 = new Case(Subject='test', Client_ID__c = 'test', Contact_Email__c ='test@test.com');
          insert c1;
         case c3=new case();
         c3.Subject='test';
         c3.Client_ID__c='text';
         c3.Contact_Email__c='test@test.com';
         c3.OwnerId=g.Id;
        insert c3;
         cases.add(c3);
         cases.add(c);
         cases.add(c1);
        
         List<Id> caseIds = new List<Id>();
         caseIds.add(c.Id); 
          caseIds.add(c1.Id);
          caseIds.add(c3.Id);
         
        Test.startTest();
        Boolean hasErrors=true;
        PageReference pageRef = Page.CS_AcceptOldestCase;
        Test.setCurrentPage(pageRef);
        
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(cases);  
        ssc.getHasNext();
        ssc.getHasPrevious();
        ssc.getPageNumber();
        CS_AcceptOldestCaseController con = new CS_AcceptOldestCaseController(ssc);         
        con.AcceptCase();        
        Test.stopTest();
    }
    public testmethod static void testAcceptOldestCaseController1()  {
         List<Case> cases=new  List<Case>();        
           Case c = new Case(Subject='test', Client_ID__c = 'test', Contact_Email__c ='test@test.com');
          insert c;
        Case c1 = new Case(Subject='test', Client_ID__c = 'test', Contact_Email__c ='test@test.com');
          insert c1;         
         cases.add(c);         
         List<Id> caseIds = new List<Id>();
         caseIds.add(c.Id); 
         
        Test.startTest();
        Boolean hasErrors=true;
        PageReference pageRef = Page.CS_AcceptOldestCase;
        Test.setCurrentPage(pageRef);
        
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(cases);  
         ssc.getHasNext();
         ssc.next();
        CS_AcceptOldestCaseController con = new CS_AcceptOldestCaseController(ssc);         
        con.AcceptCase();        
        Test.stopTest();
    }   
}

I have covered all part except
 while(cont.getHasNext()){
                for(Case c : (List<Case>) cont.getRecords()){
                    caseIds.add(c.Id);
                }
                cont.next();
            }
and cath part
Best Answer chosen by Amol Gaikwad 13
Ajay K DubediAjay K Dubedi
Hi Amol,
To cover Catch part of your code you can generate an exception at last in your code like:
if(Test.isRunningTest()){
    integer num = 1/0; //divide by zero exception.
}
this will cover catch part.
So to cover catch in test class change your controller as like:
public class CS_AcceptOldestCaseController {
    public String filterId {get; set;}
    public Boolean hasErrors {get; set;}
    public Case oldestCase {get; set;}
    
    // Constructor to accept oldest case
    public CS_AcceptOldestCaseController(ApexPages.StandardSetController cont){
        System.debug('---->>>' + cont);
        try {
            // get the 15 digit filter id for return information
            filterId = String.valueOf(cont.getFilterId()).substring(0, 15);
            
            // get all record ids in the list view
            List<Id> caseIds = new List<Id>();
            for(Case c : (List<Case>) cont.getRecords()){
                caseIds.add(c.Id);
            }
            system.debug('cont.getHasNext()cont.getHasNext()----' + cont.getHasNext());
            
            while(cont.getHasNext()) {
                for(Case c : (List<Case>) cont.getRecords()){
                    caseIds.add(c.Id);
                }
                cont.next();
            }
            
            // get all records in the list that have a owner of type Queue sorted by last modified date, then assign the first one to treat
            System.Debug('caseIds:' + caseIds);
            List<Case> cases = [select Id, OwnerId, CaseNumber from Case where Id=: caseIds and Owner.Type = 'Queue' order by CreatedDate ASC];
            if (cases.size() > 0){
                oldestCase = cases[0];
            } else {
                oldestCase = null;
            }
            
            hasErrors = false;
            if(Test.isRunningTest()){
                integer num = 1/0; //divide by zero exception.
            }
        } catch (Exception ex){
            hasErrors = true;
            ApexPages.addMessages(ex);
        }
        
    }
    
    // accept the case
    public PageReference acceptCase(){
        try {
            String successMsg = 'No queue cases found';
            if (oldestCase != null){
                oldestCase.OwnerId = UserInfo.getUserId();
                update oldestCase;
                
                successMsg = 'Case ' + oldestCase.CaseNumber + ' accepted';
            } else  {
                hasErrors = true;
            }
            
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,successMsg);
            ApexPages.addMessage(myMsg);
            if(Test.isRunningTest()){
                integer num = 1/0; //divide by zero exception.
            }
            
        } catch (Exception ex){
            hasErrors = true;
            ApexPages.addMessages(ex);
        }
        
        return null;
    }
    
}
By using this your test class cover 92% of your controller.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi