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
sp13sp13 

how to include catch in test class

this is my apex class:
public with sharing class TerminalsCX {

    public TerminalsCX(ApexPages.StandardController controller) {}
  
    public List<Terminal__c> getTerminals() {
        try {
            return[Select Name, Location__c from Terminal__c ORDER BY Name ASC];
        } catch(Exception e) { apexPages.addMessages(e); } return null;
    }
}

and this is the test class:
@isTest
public class TerminalsCX_Test {

    @isTest(SeeAllData=true)
    public static void TerminalsCX() {
        Terminal__c terminal = new Terminal__c(Name='Terminal Test');
        insert terminal;
       
        Test.startTest();
            TerminalsCX controller = new TerminalsCX(new ApexPages.StandardController(terminal));
            controller.getTerminals();
        Test.stopTest();
    }
}

when i run the test the whole line of "} catch(Exception e) { apexPages.addMessages(e); } return null;" is not included, so this is just 80%, how can i make it 100%? how can i include catch in the test class?
mauro_offermannmauro_offermann
I think the method getTerminals never fall into the catch because if the query does not deliver results you return an empty list and if the query brings more than 50,000 records throw an exception limits that are not caught by the catch. I add the LIMIT 50000 clause to the SELECT and would take the catch.
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
Yes, the only way that could ever fail is if you have more than 50k records in that object.  Doesn't seem likely to ever happen from what I am seeing from your page, although I would suggest putting either some limit or filter just to make sure and do what the above suggested and remove the try/catch block.