0

How can i catch this method in apex test class

  public static void CountparentRetirementAccountUpdate1(List<Holding__c> holdings){
    List<id> reaccid=new List<id>();
    List<id> fundid=new List<id>();
    for(Holding__c hol : holdings){
        reaccid.add(hol.Retirement_Account__c);
        fundid.add(hol.Fund__c);
    }
    List<Retirement_Account__c> retacclist = [select id,Account_Summary__c,(select id,Amount__c,Fund__r.Category__c from Holdings__r ORDER BY Amount__c DESC,Fund__r.Category__c ASC LIMIT 3) from Retirement_Account__c where id IN: reaccid ];
 try{for(Retirement_Account__c r:retacclist){   
     String category='';   
  for(Holding__c h:r.Holdings__r){  
      String s;           
      if(h.Fund__c!=null && h.Fund__r.Category__c!=null){
          s= h.Fund__r.Category__c;           
      }
      else{
          s='Unknown';
      }
      category += (category==''?'':',')+s;              
       System.debug('ss'+s);
  }          
      r.Account_Summary__c =category;  

   system.debug('#####'+retacclist);     
    } 
     update retacclist; 
     List<Fund__c> fundList=[select id,Total_Shares_Outstanding__c,(select 
  id,Fund__c,Shares__c from Holdings__r) from Fund__c where id IN:fundid];
   for(Fund__c fList:fundList)
    {
        decimal sum=0;           
        for(Holding__c holdList:fList.Holdings__r)
        {
             holdList.Fund__c=fList.Id;
            if(holdList.Shares__c!=null){                   
            sum +=holdList.Shares__c; 
            }
        }
       fList.Total_Shares_Outstanding__c =sum;         

    }
           update fundList;
    }
     catch(Exception e)
    {
        system.debug(e);
    }

}
0

On the assumption that you mean how can you tell that the method caught and debug logged an exception while in a unit test, this is most easily done by doing something like:

} catch (Exception e) {
    System.debug(e);

    if (Test.isRunningTest()) {
        throw e;
    }
}

I.e. only when executing a test is the exception re-thrown.

0

You can mock a private testVisible variable in your main class and then set its value in the test class to see if the exception has occurred. The advantage of mocking from Test.isRunningTest() is you are in control when an exception is thrown. For some testing method, you don't want this behavior thus you can bypass exception throwing.

public with sharing class MyApexClass {


    @TestVisible
    private static boolean enableExceptionThrowing = false;


    public static void CountparentRetirementAccountUpdate1(List<Holding__c> holdings) {
        List<id> reaccid = new List<id>();
        List<id> fundid = new List<id>();
        for (Holding__c hol : holdings) {
            reaccid.add(hol.Retirement_Account__c);
            fundid.add(hol.Fund__c);
        }
        List<Retirement_Account__c> retacclist = [select id,Account_Summary__c, (select id,Amount__c,Fund__r.Category__c from Holdings__r ORDER BY Amount__c DESC, Fund__r.Category__c ASC LIMIT 3) from Retirement_Account__c where id IN:reaccid];
        try {
            for (Retirement_Account__c r : retacclist) {
                String category = '';
                for (Holding__c h : r.Holdings__r) {
                    String s;
                    if (h.Fund__c != null && h.Fund__r.Category__c != null) {
                        s = h.Fund__r.Category__c;
                    } else {
                        s = 'Unknown';
                    }
                    category += (category == '' ? '' : ',') + s;
                    System.debug('ss' + s);
                }
                r.Account_Summary__c = category;

                system.debug('#####' + retacclist);
            }
            update retacclist;
            List<Fund__c> fundList = [
                    select id,Total_Shares_Outstanding__c, (
                            select
                                    id,Fund__c,Shares__c
                            from Holdings__r
                    )
                    from Fund__c
                    where id IN:fundid
            ];
            for (Fund__c fList : fundList) {
                decimal sum = 0;
                for (Holding__c holdList : fList.Holdings__r) {
                    holdList.Fund__c = fList.Id;
                    if (holdList.Shares__c != null) {
                        sum += holdList.Shares__c;
                    }
                }
                fList.Total_Shares_Outstanding__c = sum;

            }
            update fundList;
        } catch (Exception e) {
            system.debug(e);
            if(enableExceptionThrowing)
                throw e;
        }

    }




}

Test class:

private static void unitTestForException(){

    MyApexClass.enableExceptionThrowing = true ; 

    MyApexClass.CountparentRetirementAccountUpdate1(records); //This should throw exception in your test class



}

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.