Thursday, 20 July 2023

Salesforce's New Assert Class


Introduction To The New Assert Class

  • The Assert class is designed to provide clearer assertions within Apex test code and make it easier for developers to write more maintainable test code.
  • This new class offers multiple static methods and is intended to be a dedicated location for assert methods.
  • The addition of this class means that developers will have all the assert methods they need in one place.
  • Starting with “Assert” is more intentional and readable, making the test code cleaner.
  • Although Apex still allows the use of System assert methods.


Code Example of New Assert Class

  • The above code demonstrates the use of Old and new Equality assertions and Expression true Assertion. 
  • As in the code, we are using the same system assert method for Equality and Expression true assertion using different statements while the new Assert class provides two different methods for Equality and Expression true assertion which makes the code more intentional.


  • The above code represents the use of Not null and Instance of type assertions where the use of these two assertions makes the code more readable. 
  • There are two other assertion methods in the new assert class such as Assert.isNull(value, msg), Assert.isNotInstanceOfType(instance, System.notExpectedType, msg). 
  • If you noticed, like old assertion methods, the new Assertion methods also include an assertion message. 

  • The above test class method includes the Error Testing Assertions where the same assert method is used for error testing in the old assertion way but the new assert class provides the specific method. 


Conclusion

The new assert class provides an improved and more efficient way to write assertions, resulting in better testing and higher code quality.



If you have any questions you can reach out our Salesforce Consulting team here.

No comments:

Post a Comment