Revamp Your Salesforce Testing with the New Assert Apex Class in Winter 23 Release!

Revamp Your Salesforce Testing with the New Assert Apex Class in Winter 23 Release!

The Winter 23 Salesforce release has brought a game-changing addition to the platform - the Assert Apex Class. This class includes multiple static methods that offer clearer assertions within Apex test code, ensuring developers write more maintainable test code.

Let's take a closer look at some examples and compare them to the old methods.

Expression True Assertions

Expression True Assertions used to require developers to write "system.assert" before each function, but with the new Assert Class, developers can use the simpler "Assert.isTrue" function, which provides cleaner and more intentional code.

Old way
Boolean containsHello = 'Hello World'.contains('Hello'); 
system.assert(containsHello, 'The message does not contain Hello');
New way
Boolean containsHello = 'Hello World'.contains('Hello');
Assert.isTrue(containsHello, 'The message does not contain Hello');

Equality Assertions

Equality Assertions now use "Assert.areEqual" instead of the older "system.assertEqual" method. Similarly, Not Null Assertions now use "Assert.isNotNull" instead of the old, confusing options.

Old way
Integer a = 3; 
Integer b = a; 
System.assertEqual(a, b, 'a and b are not equal.');
New way
Integer a = 3; 
Integer b = a; 
Assert.areEqual(a, b, 'a and b are not equal.');

Is Null Assertion

Is Null Assertions have also undergone a change, with "Assert.isNull" replacing the older "system.assertEquals" method. Even error testing has been revamped, and developers can now use the "Assert.fail" function to check for expected errors, providing clearer, more concise code.

Old way
Object result = calculate(input);
system.assertEquals(null, result, 'The result is not null.');
New way
Object result = calculate(input); 
Assert.isNull(result, 'The result is not null.');

Not Null Assertion

Old way
Object instance = create(); 
// Option 1 
system.assert(instance != null, 'The instance is null.'); 
// Option 2 
system.assertNotEquals(null, instance, 'The instance is null.');
New way
Object instance = create(); 
Assert.isNotNull(instance, 'The instance is null');

Error Testing

Old way
Exception expectedException = null; 
try { 
     // Do something here that should cause an exception 
    } 
catch (Exception ex) { 
    expectedException = ex; 
    // Assert the expected error occurred and not a different one. 
    } 
system.assert(expectedException != null, 'The expected error did not happen.');
New way
Exception expectedException; 
try { 
     // Do something here that should cause an exception 
     Assert.fail('The expected error did not happen'); 
    } 
catch (Exception ex) { 
     expectedException = ex; 
     Assert.assertEquals('Expected error message', expectedException.getMessage()); 
    }

Conclusion

Overall, the new Assert Class provides better-named methods for all common test assertion cases, resulting in cleaner, more intentional code. While refactoring existing test code solely to use these new methods is not recommended, using them in new test code or during code refactoring can make a significant difference.

It's crucial to ensure that test code always includes assert messages, even with the new methods' improved clarity, to guarantee consistent testing. The 75% test code coverage requirement will naturally be met with good tests, so always remember that the test code's purpose is to ensure the system behaves as expected.

Are you excited to revamp your Salesforce testing with the new Assert Apex Class? Let us know your thoughts in the comments below!

Want To Learn More?

Learn about How to create a responsive Visualforce page
Read more about Assert Class

Share this article...

Salesforce Mentor, with 10 years Salesforce experience, Hardcore Admin & Guru Developer, Geek, Animal lover, and dog & cat rescuer activist. Lifetime student, Stand-Up comedian wannabe, Photographer, Gamer, Anime lover, and coffee addict. Spreading Salesforce love and teaching beyond my capacity. Aiming to become Tech Architect!

One Ping

  1. Pingback: Unlock the Power of Large Data Sets in Salesforce with PK Chunking - Boost Your Performance Today! - SalesforceMonday

Leave a Reply

Your email address will not be published. Required fields are marked *