Debug with Assert class

However, debugging with the Assert class is a straightforward and efficient way to debug both Apex and Apex Unit Tests.

How to debug code with the Assert class?

There are numerous Apex debugging methods.

However, debugging with the Assert class is a straightforward and efficient way to debug both Apex and Apex Unit Tests.

apex
@IsTest
static void myTestMethod() {
    Account acc = [SELECT Id, Name FROM Account WHERE Name = 'TestAcc'];

    Assert.areEqual(new Account(), acc); // This will fail, and you will see the value

    List<Contact> contacts = MyController.getAccountContacts(acc.Id);
    ...
}
bash
System.AssertException: Assertion Failed:
Expected: Account:{},
Actual: Account:{Id=0013M00001HrGndQAF, Name=Beyond The Cloud}

Why can it be useful?

Debug Unit Test - You can view System.debug statements in Apex, but not in Apex Unit Tests. Adding an Assert statement allows you to inspect the variable values. Debug in CICD - Encounter issues in your CI/CD process? Tests work in the development environment but fail during deployment? Adding an Assert statement can help you debug these issues.

Text and code were extracted from the original slide. Plain-text version of the whole catalog