0
public class Zuora_Account implements NGTrigg.Executor {
   private List<Subscription__c> newSubscriptionList;
     private static Boolean HAS_BEEN_EXECUTED = false;
     public static final String CLASSNAME = 'Zuora_Account';

     public Zuora_Account(List<Subscription__c> newSubscriptionList){  
        system.debug('Enter Zuora_AccountUpdate constructor>>>>>>>>>>>>>>>>');     
        this.newSubscriptionList = newSubscriptionList;
     }

     public void execute() {
        Set<Id> acctIds = new Set<Id>();
        List<Account> acctToBeUpdated = new List<Account>();    
        try{
            if(HAS_BEEN_EXECUTED){
                return;
            }
            for (Subscription__c subscription : newSubscriptionList) {
                acctIds.add(subscription.Zoura_Account__c);
            }
            if( acctIds.size() > 0) {
                for(Account acctObj : [select DO_NOT_MERGE__c from account where id in : acctIds]) {

                    if( !acctObj.DO_NOT_MERGE__c || acctObj.DO_NOT_MERGE__c == false) {
                        acctObj.DO_NOT_MERGE__c = true;
                        acctToBeUpdated.add(acctObj);
                    }
                }
                if (!acctToBeUpdated.isEmpty()) {
                    system.debug('update account do not merge flag: ' + acctToBeUpdated);
                    update acctToBeUpdated; 
                }                   
            }                  
            HAS_BEEN_EXECUTED = True;
        }
        catch(Exception ex){
            String errMsg = ex.getMessage()+' at line '+ex.getLineNumber().format()+' in '+CLASSNAME;
            system.debug('Error:'+errMsg); 
            Log__c erroLog = new Log__c ();
            erroLog.Type__c = ex.getTypeName();
            erroLog.Log__c = ex.getMessage();
            erroLog.Business_Unit__c = CLASSNAME;
            insert erroLog;    
        }
     }
}

and test class

{@isTest
public class Test_ZouraAcc {

    public static testmethod void test_z_Acc(){

        List<Account> testAccounts = new list<account>();
        account acc1=new account();
        acc1.name='test1';
        acc1.DO_NOT_MERGE__c =false;
        testAccounts.add(acc1);
        account acc2=new account();
        acc2.name='test1';
        testAccounts.add(acc2);
        insert testAccounts;


        List<Subscription__c> zsubList =new list<Subscription__c>();
        Subscription__c sc1=new Subscription__c();
        sc1.name = 'TestSub1' ;
        sc1.Zoura_Account__c=testAccounts[0].id;
        zsubList.add(sc1);

        Subscription__c sc2=new Subscription__c();
        sc2.name = 'TestSub2' ;
        sc2.Zoura_Account__c=testAccounts[1].id;
        zsubList.add(sc2);
        insert zsubList;
        Zuora_Account za = new Zuora_Account(zsubList);
        za.execute();

    }
}
}

I am not able to cover Catch block Please help me

0

Looking at the above code I think 1 way of covering the catch block exception is to throw custom exception only to cover the catch block.

  1. Create a new class that implements Exception which will be used to throw the custom exception from the Zuora_Account class execute() method.

    public class MyException extends Exception {}
    
  2. Add static Boolean crashTest variable in Zuora_Account class.

  3. In execute() method in Zuora_Account class add below code

    if(Test.isRunningTest() && crashTest) {
        throw new MyException('Exception');
    }
    
  4. Add another test method and set the flag of crashTest = true and call the execute() method that will cover the catch block exception
| improve this answer | |
  • But i cannot edit main class..Is there any other way by doing changes in Test class only? – dem Nov 24 '16 at 9:33
  • I don't think so there is any other way until & unless you try to set some value from the test class which will then trigger the exception. But i don't find any way of doing this in above code. Salesforce that's way say we require atleast 75% code coverage and not 100% because in some cases you can't cover the code. Only way it to change the main class and throw custom exception. – RCS Nov 24 '16 at 9:37

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.