Wednesday, June 18, 2014

How to use System.assert in Salesforce

So you are writing code and trigger a lot but as a mandatory step you need to have at lest 75% or greater of production code coverage to move your code. "Ok no problem I will write a test class for this trigger/class" will be your answer, but what happen if your code contains try catch block or you have some value returning from a method that you want to cross check. To cover the above scenario we can take help from "System.assert" method provided by salesforce.

Reference Link

Say I have a validation rule on opportunity that will only allow me to insert opportunity records if the probability is more then 50%. And I am inserting an "Opportunity" at the time of "Account" creation:


Now in my above if I try to give probability less then 50 then error will occur and my test will fail. To avoid this in my test class I can use try catch and inside catch I can use assert like this:

Hope this helps you.

No comments:

Post a Comment