You need to sign in to do that
Don't have an account?
Markey1
How to test Catch Blocks
Anyone know how to test Catch blocks. I've looked around for creating Catch test conditions but have been unsuccessful so far.
Code Snippet:
insert skEnroll; }catch (DMLException e) { ApexPages.addMessages(e); return null; }return page.aeskenrollconfirm; } }
What you could do in your test class is try to force the controller to insert an invalid record. E.g. if your try block has "insert skEnroll", and you have control over what fields are populated before skEnroll is inserted, try omitting a required field, .e.g. Name. That should throw an error and trigger the catch block.
Thanks for the reply. I'm using the SFDC UI (not Eclipse or Force.com IDE) and when I try to force an error, it is caught by the UI and actually reduces my coverage. I highlighted the rows that are causing issues in the class and you will see this is not the only peice lacking coverage . Any help you can provide with the below test class is much appreciated.
Class AESK Test:
Test Class:
I am facing same problem. I am developing in Salesforce UI. I want to cover a sample catch block for DML Exception.
I have 70% coverage which excludes only the statements under catch block.
When I use -ve use case in test method it throws me error and fails the test resulting 0% covering.
When I use try catch within test method (using asserts to expect DML exception the test class runs - but still leaves me with 70% coverage excluding the catch statements in original trigger.
Can anyone of you let me know how did you solve your porblem?
Thanks in advance!