Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.

Hi all!

My first question, apologies in advance if answer is clear, but I am beginning with APEX, and I am a bit stuck here.

What does "System.Assert(false);" is doing exactly here?

try{

ContactsDML.databaseMethodDML();

System.Assert(false);

} catch(DMLException e){

System.assert(e.getMessage().contains('Required fields are missing: [LastName]: [LastName]'));

}

I am within a class method, calling another class for the test, which results in an error (as stated in the message).

My question is because I do not see any changes, whether I use true or false on that parameter:  System.Assert(false);

Thanks in advance!

 
7 answers
  1. Jan 8, 2021, 10:40 PM
    Hi Anudeep, I double-checked with other sources and indeed this kind of assessment is used to ensure an error is given so you can catch the exception. That is the purpose. Thanks!
  2. Dec 2, 2020, 11:09 AM
    Did you try using System.Assert(false) in the try statement? What was the result
  3. Nov 30, 2020, 2:37 PM
    You could give it a try. The documentation says you can’t catch an assertion failure using a try/catch block even though it is logged as an exception though
  4. Nov 30, 2020, 8:41 AM
    Hi Anudeep!

    Thanks, I have a second thought

    What about "System.Assert(false)" is within the try statement to ensure force error

     
  5. Nov 26, 2020, 3:44 PM
    Hi Anudeep, Thanks for the explanation, I also reviewed the documentation

    My specific question is what that piece of code ( System.Assert(false); is doing on the whole picture, why it is there because I do not see any changes wether I set it to true to false.

    So, by your answer, I understand that it is not doing anything, and the code should be like this instead

     

    try{

    ContactsDML.databaseMethodDML();

    // System.Assert(false);

    } catch(DMLException e){

    System.assert(e.getMessage().contains('Required fields are missing: [LastName]: [LastName]'));

    }

    Did I understand you right?

     
  6. Nov 26, 2020, 1:15 PM
    As per the documentation, System.Assert asserts that the specified condition is true. If it is not, a fatal error is returned that causes code execution to halt. You should ideally only provide a condition to a system. assert

     

    System.assert(condition, msg);

    System.Assert accepts two parameters, one (mandatory) which is the condition to test for and the other a message (optional) to display should that condition be false.

    System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false.

    Now, testing best practices state that you should assert some condition but also output a “console” message that shows the values of each of the operands. This certainly helps you debug unit tests when asserts are failing

    Here is an example showing proper usage

    @isTest

    public class SampleTestClass {

    @isTest

    static void example(){

    List<Account> accountsToBeCreated = new List<Account>();

    //Create test data

    for(Integer i = 0; i<10; i++){

    Account newAccount = new Account();

    newAccount.name='test'+i;

    newAccount.Industry = 'Energy';

    accountsToBeCreated.add(newAccount);

    }

    //Insert test Data

    insert accountsToBeCreated;

    //Query test data that was just created

    List<Account> createdAccounts = [SELECT Id, Industry From Account];

    //Assert list is not empty

    System.assert(!createdAccounts.isEmpty(),'List should not be empty');

    //Ensure 10 records were created

    System.assert(createdAccounts.size() == 10,'10 account records should be returned');

    //Loop through accounts ensure industry is not null

    for(Account acc : createdAccounts){

    System.assert(acc.Industry != null,'Industry should not be null');

    }

    }

    }

    Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!

     
0/9000