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
Olga Kim 7Olga Kim 7 

Test "Try and Catch" block Code

Hi all,
I wrote a test class (please, see below) that covers most of my code except "catch" part of the code.
Please, help me to figure out what is missing in my apex test class.
public with sharing class SubmitReportUseOfAwardController {
    
    @AuraEnabled
    public static string updateUseOfAward(string recordId){
  
   Use_of_Award__c record=[SELECT id, UOA_Report_Status__c FROM Use_of_Award__c WHERE Id=:recordId];        
       try
    {
    
        record.UOA_Report_Status__c='Submitted';
        update record;
        
        return 'SUCCESS';
    }
        catch(Exception e){
        throw new AuraHandledException(e.getMessage()); 
      }
    }
}

My test class
@isTest
public class SubmitReportUseOfAwardCtrTest {

    @testSetup
    public static void dataInit()
    {
        Account org=new Account();
        org.Name='test';      
        insert org;
        
        Award__c award=new Award__c();
        award.Organization__c=org.Id;
        insert award;
        
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        User u1 = new User(Alias = 'standt1',
                           Country='USA',
                           Email='demo1@randomdemodomain.com',
                           EmailEncodingKey='UTF-8',
                           LastName='Testing',
                           LanguageLocaleKey='en_US',
                           LocaleSidKey='en_US',
                           ProfileId = p.Id,
                           TimeZoneSidKey='America/Los_Angeles',
                           UserName='dprobertdemo1@camfed.org');
        insert u1;
        
        Use_of_Award__c record=new Use_of_Award__c();
        record.UOA_Award__c=award.Id;
        record.Fiscal_Year_Entry__c='2020';
        record.CCME_Analyst__c=u1.Id;
        insert record;
        
    }
    
       
     @isTest
    public static void testUpdateUseOfAwardPositive()
    {
    
        
        string recordId=[SELECT Id from Use_of_Award__c LIMIT 1].id;  
        string result=SubmitReportUseOfAwardController.updateUseOfAward(recordId);
        system.assert(result=='SUCCESS');
        

    }
       @isTest
    public static void testUpdateUseOfAwardNegative()
    {
        try
        {
          SubmitReportUseOfAwardController.getUseOfAwardRecord(null);
            system.assert(false);
        }
          catch(exception e)
        {
           system.assert(true); }
     
    }
    
}

​​​​​​​
Andrew GAndrew G
Hi

The major challenge is that the code is quite simple it what it does.  It takes a record Id, finds a record and updates a field.  You need to find a way to cause the update to fail.  

Since the code is doing all the updates, its not like you can remove a required field to cause a required field validation to fire.

Options to try would be:
1. pass a 'fake' record Id to the class to see if your error capture works.
2. if you are running a Private sharing model where the record is read only, set up the test to run as a user with no access to edit the file.

Just some brief ideas to try.

regards
Andrew