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
Mike ChandlerMike Chandler 

How can you exclude a block of code from some unit tests but not from others?

I know that it's possible to exclude a block of code from being executed with creative use of the following method:
Test.isRunningTest()
However, I was hoping someone knew of a strategy for excluding a block of code from certain unit tests while allowing it to run in others.  I have portions of a Trigger that I want to run for certain unit tests, but I'd prefer not to run the same portion of the trigger for other tests. Is there a common strategy for doing that?  Any help would be greatly appreciated.  Many thanks!
Best Answer chosen by Mike Chandler
KapilCKapilC
Hi Mike,

You have to created a TriggerHandler class and put a static varibale in it. When running the test class you can switch it on/off. Please find the code attached.

Triggger:-
trigger AccountTrigger on Account (before insert, before update) {
	if(trigger.isInsert){
		AccountTriggerHandler.beforeInsert(trigger.new);
	}else{
		AccountTriggerHandler.beforeUpdate(trigger.newMap,trigger.oldMap);
	}
}

Class:-
public without sharing class AccountTriggerHandler{
 public static boolean canRunFromTestClass {get;set;}
public static void beforeInsert(list<account> newAccountList){
  for(account acc : newAccountList){
        if(test.isRunningTest() && canRunFromTestClass == true){
            acc.description = 'Inserted By Test Class.';
        }
    }

}
public static void beforeupdate(map<id,account> newAccountMap,map<id,account> oldAccountMap){
  for(account acc : newAccountMap.Values()){
        if(test.isRunningTest() && canRunFromTestClass == true){
            acc.description = 'Updated By Test Class.';
        }
    }
}
}

Test Class:-
@isTest
private class AccountTest1 {

    static testMethod void myUnitTest() {
    	
       account acc = new account(name='Test Acc');
       insert acc;
       // Not inserting description because we don't want that line of code excuted from here.
       system.debug(':::::::::'+[select description from account where id=: acc.id]);
       
       acc.name = 'Test Acc Up';
       // Trun flag on 
       AccountTriggerHandler.canRunFromTestClass = true;
       update acc;
       // updating description because we want that line of code excuted from here.
       system.debug(':::::::::'+[select description from account where id=: acc.id]); 
    }
}
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)
​​

All Answers

KapilCKapilC
Hi Mike,

You have to created a TriggerHandler class and put a static varibale in it. When running the test class you can switch it on/off. Please find the code attached.

Triggger:-
trigger AccountTrigger on Account (before insert, before update) {
	if(trigger.isInsert){
		AccountTriggerHandler.beforeInsert(trigger.new);
	}else{
		AccountTriggerHandler.beforeUpdate(trigger.newMap,trigger.oldMap);
	}
}

Class:-
public without sharing class AccountTriggerHandler{
 public static boolean canRunFromTestClass {get;set;}
public static void beforeInsert(list<account> newAccountList){
  for(account acc : newAccountList){
        if(test.isRunningTest() && canRunFromTestClass == true){
            acc.description = 'Inserted By Test Class.';
        }
    }

}
public static void beforeupdate(map<id,account> newAccountMap,map<id,account> oldAccountMap){
  for(account acc : newAccountMap.Values()){
        if(test.isRunningTest() && canRunFromTestClass == true){
            acc.description = 'Updated By Test Class.';
        }
    }
}
}

Test Class:-
@isTest
private class AccountTest1 {

    static testMethod void myUnitTest() {
    	
       account acc = new account(name='Test Acc');
       insert acc;
       // Not inserting description because we don't want that line of code excuted from here.
       system.debug(':::::::::'+[select description from account where id=: acc.id]);
       
       acc.name = 'Test Acc Up';
       // Trun flag on 
       AccountTriggerHandler.canRunFromTestClass = true;
       update acc;
       // updating description because we want that line of code excuted from here.
       system.debug(':::::::::'+[select description from account where id=: acc.id]); 
    }
}
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)
​​
This was selected as the best answer
Mike ChandlerMike Chandler
Kapil,

Why on earth didn't I think of that?  It seems so obvious to me now that I'm reading it! :) Excellent answer, Kapil.  Thanks for sharing this!

Regards,

Mike