+ Start a Discussion
Carlo NewCarlo New 

Apex Tests for Workflow Email Alerts



Is there a to test a workflow email alert from an apex test.  I know tests do not send emails.  Can I test if the email alert was actioned or called in some way?





yep if you put a debug ...you will able to see all the components which are effected w.r.t your test class execution... you will find workflow also in the debug......
Carlo NewCarlo New

Would I have to search the debug log for the Workflow Alert name?


We have a 16 stage Opportunity process and we also have 6 different Business Units with different types.  At each stage we want to alert various users and non-users based on the Business Unit and type.  I want to use an apex test script to create an opportunity, update values and the stage from 1 to 16.  I want to use an apex test script to check all the validation rules.  This I think is achievable.  I would also like to know if I can test the Workflow Alerts before I set about doing this.  If I can not then it seems like I am only testing half of our process.


If by "test" you mean, get you workflow to execute and see something in the debug log. The answer is yes. Your debug log has different log categories and one of them is workflow. You'll want to dial the logging category for workflows right up and then look for those message types in your log. You will also want to turn down all other log categories for that test, otherwise you risk reaching the max log size and you might not see all the information you wish. 


If by "test" you mean, prove that it works based on parameters entered and be notified on incorrect behavior, the answer is no. A real testing framework would allow you to place tested conditions, and have the tests report back expected positive and negative behaviors based on those conditions. Here is where the Apex unit testing framework won't help you. You can get the debug logs, but it will be entirely up to you to interpret those logs. There is no way for you to test the outcome of the Workflow Email Alert action from within the Apex context (this email was sent as a result of this parameter). 


Testing for validation rules is a different story. This would be relatively easy as validation rules, when failing, throw the DMLException. So all you need to do is create rubbish data, attempt a save in a try block, then test for DMLException in the catch. Additionally you can test for the error message(s). This process would be fundamentally the same as writing a test for an Apex validation, only for a validation rule test, I wouldn't ever bother with bulk tests. 


Hope this gives you some stuff to go on. 

the best thing that you can do in this case.... click on filters link in the debug section and put select info for workflows only and put rest of the Categories level to none.. so that you will find only workflow alerts info and it is easy to check whether a workflow is fired or not....

1. check with name
2. see the WF_CRITERIA_END..whether it is giving true or false to check the workflow triggered the action or not.....
so by observing these you can easily cross check...
Carlo NewCarlo New

Ok thanks

Bhawani SharmaBhawani Sharma
You can not put assert statement for this, but definitely you can see that from your debug log.
I am not very much sure if you can query Email Message table to see if there was any new record inserted.