0

Below apex class is not covering catch block. Due to thi i'm unable to migrate the below class to other environments.Can anyone help me to cover the catch block. Now it's covered 75%.
Apex Class:

    @AuraEnabled
        public static List<RecordType> getListOfRecordType(){
            String businessRT = Label.Account_RecordType_Business_Devname;
            String partnerRT =  Label.Account_RecordType_COI_Devname;        
            try{
                 return [SELECT Id, toLabel(Name), toLabel(Description) FROM RecordType 
                                        WHERE (IsActive=TRUE AND 
                                            SobjectType = 'Account' AND
                                            Name != 'Master') AND
                                            (DeveloperName = :businessRT OR DeveloperName = :partnerRT)
                                            ];

            }catch(Exception e){
                return null; 
            }    
        }

    }

Apex Test Class:

    static testMethod void RecordTypeSelectorControllerTestMethod() {
            List<Trigger_Bypass__c> tSwitch = TestDataFactory.createTriggerSwitch();              
            Database.insert(tSwitch);           
            Schema.DescribeSObjectResult recordType = Schema.SObjectType.Account; 
            Map<String,Schema.RecordTypeInfo> AccountRecordTypeInfo = recordType.getRecordTypeInfosByName();
            Id rtId = AccountRecordTypeInfo .get('Business').getRecordTypeId();        
            Account Acc = new Account(Name='Master',recordtypeid=AccountRecordTypeInfo .get('Business').getRecordTypeId());       
            insert Acc;
            Test.startTest();        
            RecordTypeSelectorController.getListOfRecordType();
            Test.stopTest();
        }
3
  • 2
    Why do you need that catch block at all? What exception do you anticipate this query will throw?
    – David Reed
    May 7 '18 at 13:17
  • If the class will migrate to other environments, suppose there is no recordtype with that name..we can handle the exception in catch block. That purpose i have used try catch block..
    – Chaitu
    May 7 '18 at 13:19
  • 2
    You will not get an exception if there is no record type of the given name. The query will return an empty list.
    – David Reed
    May 7 '18 at 13:21
1

There's no exception here you could possibly catch; it is recommended that you remove the try-catch block. The fact that you can't write a proper unit test that covers it is proof that you can't catch anything here.

However, if you were to run in to a situation like this where a try-catch would be effective, you'd want to move the return value to a variable that you'd return at the end, like this:

@AuraEnabled
    public static List<RecordType> getListOfRecordType(){
        String businessRT = Label.Account_RecordType_Business_Devname;
        String partnerRT =  Label.Account_RecordType_COI_Devname;
        RecordType[] results;        
        try{
             results = [SELECT Id, toLabel(Name), toLabel(Description) FROM RecordType 
                                    WHERE (IsActive=TRUE AND 
                                        SobjectType = 'Account' AND
                                        Name != 'Master') AND
                                        (DeveloperName = :businessRT OR DeveloperName = :partnerRT)                ];

        }catch(Exception e){
        }
        return results;
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.