Write targeted apex assertions in test classes

Salesforce winter 23 release, introduced new System.Assert class to provides methods that handle all types of logical assertions and comparisons. with this new assertions methods, your Apex code is easier to read and understand.

Still you can user old assert methods. System.assert(), System.assertEquals(), and System.assertNotEquals().

New list of assert class methods

Assert method Description
Assert.areEqual(expected, actual)Asserts that the two arguments are the same.
Assert.areNotEqual(notExpected, actual)Asserts that the two arguments aren’t the same.
Assert.isTrue(condition)Asserts that the specified condition is true.
Assert.isFalse(condition)Asserts that the specified condition is false.
Assert.isNull(value)Asserts that the value is null.
Assert.isNotNull(value)Asserts that the value isn’t null.
Assert.sInstanceOfType(instance, expectedType)Asserts that the instance is of the specified type.
Assert.isNotInstanceOfType(instance, notExpectedType)Asserts that the instance isn’t of the specified type.
Optionally you can add messages in each method.

Using new assertion methods in apex test

how we are using system.assert methods, same way we can use these new Assert class methods.

String sub = 'abcde'.substring(2);
Assert.areEqual('cde', sub); 

Source:
https://developer.salesforce.com/docs/atlas.en-us.240.0.apexref.meta/apexref/apex_class_System_Assert.htm

Labels:
Join the conversation