Find your content:

Search form

You are here

Is there a way to disable System.assert assertions?


Is there a way to stop System.assert calls from executing throughout an org by a setting or programmatically?

I did not see anything in the Apex System class docs and the docs explicitly state that an assertion failure generates an exception that cannot be caught in a try/catch.

Right now, if I have code such as the following in my sandbox

public class MyController() {
    public MyController() {
        System.assert(false, 'Fail');

it will result in the following being displayed when the VF page is rendered.

Assertion Failed: Fail

An unexpected error has occurred. Your development organization has been notified.

Java assert has had that capability. See Programming With Assertions - Enabling and Disabling Assertions, which is why I'm asking.

I know that it is debatable whether it is good practice or not to turn off assertions in production code, but I'm just interested in knowing how this can be done.

Attribution to: Peter Knolle

Possible Suggestion/Solution #1

Not that I can see either sadly...

Only thing I can recommend is wrapping some of the assert methods you use in a small framework like the one I've shown below. So that you then only call the System.assert methods in your tests methods, sandbox runtime or when explicitly configured otherwise.

To use the code below create a custom setting called Asserts with a field called Enabled in your production org (no need in sandbox, as its read dynamically). Once in production, you can then enable asserts at user, profile or org level, using the standard custom setting UI.

enter image description here

Then use this code instead of the System.assert methods..

Assert.equals('Night', 'Day', 'Its the difference between...');

Here is the code to dynamically check for your sandbox and custom settings.

public with sharing class Assert 
    public static String MY_PRODUCTION_ORG_ID = '00ZX0000000CinBOBX';

    private static boolean Enabled = false;

        // Enable for test and certain runtime contexts (such as sandbox)?
        Enabled = Test.isRunningTest() || UserInfo.getOrganizationId() != MY_PRODUCTION_ORG_ID; 
            // Enabled via custom setting specified at user, profile or org level?
            Set<Id> locations = new Set<Id> 
                { UserInfo.getUserId(), UserInfo.getProfileId(), UserInfo.getOrganizationId() };
            try {
                List<SObject> enabledSettings = Database.query(
                    'select Id From Asserts__cX ' + 
                        'where SetupOwnerId in :locations and ' + 
                               'Enabled__c = true');    
                Enabled = enabledSettings.size() > 0 ? true : false;
            } catch (Exception e) { }   

    public static void equals(Object x, Object y, Object msg)
            System.assertEquals(x, y, msg);

Assertion Failed: Its the difference between...: Expected: Night, Actual: Day

An unexpected error has occurred. Your development organization has been notified.


Attribution to: Andrew Fawcett

Possible Suggestion/Solution #2

I tried wrapping it in a try-catch and sure enough was presented with the same outcome you reported. Which makes me think, this will need something home grown, such as a Custom Setting (which would require SeeAllData=true, if the intent is to also disable asserts in a Test Context - if not CustomSettings are recommended), so maybe a Custom Label which controls the running of the Assertions, and you can switch it on and off from config.

//if Assertions are not Required to be Disabled in Tests, Use A Custom Setting instead
boolean assert= Boolean.valueOf(System.Label.Assert_Assertions);

System.assertNotEquals(null, email);

It doesn't sound too appetising to pepper tests with if statements; I wonder if an AssertionDelegate is the answer

public class AssertionDelegate{

static boolean assert = Boolean.valueOf(System.Label.Assert_Assertions);

public static void assertNotEquals(Object arg1, Object arg2){
System.assertNotEquals(arg1, arg2);

AssertionDelegate.assertNotEquals('abc', 'abc');

Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content