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>();    
            for (Subscription__c subscription : newSubscriptionList) {
            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;
                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;
            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

public class Test_ZouraAcc {

    public static testmethod void test_z_Acc(){

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

        List<Subscription__c> zsubList =new list<Subscription__c>();
        Subscription__c sc1=new Subscription__c();
        sc1.name = 'TestSub1' ;

        Subscription__c sc2=new Subscription__c();
        sc2.name = 'TestSub2' ;
        insert zsubList;
        Zuora_Account za = new Zuora_Account(zsubList);


I am not able to cover Catch block Please help me

1 Answer 1


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
  • But i cannot edit main class..Is there any other way by doing changes in Test class only?
    – dem
    Nov 24, 2016 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, 2016 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.