Can we use try catch inside a test class? Is that a best practice?

Though i have not tried a test class with try catch blocks of code, i often saw people wrote try catch block inside a test class. I need expert advise as doing so is a best practice or conventional. Is there any best practices article in Salesforce docs library for writing test classes? How a test class should look like and what should or should not it contains?


This answer addresses the try/catch part of your question only.

If an exception is thrown from the code being tested (or the test code) then the test is a fail and that is normally what you want to happen.

So there is no value in adding code like this to a test:

try {
} catch (Exception e) {
    System.assert(false, 'Exception ' + e);

and as not all exceptions are catchable (e.g. governor limit ones) it will not even always execute.

(Generally code should contain relatively few try/catch expressions; it is usually better to let exceptions propagate through levels of code to be handled at the highest level. If something like a null pointer exception is happening (in code you can modify) the code should be fixed not worked around using try/catch. When designing code, throwing exceptions for expected conditions is not usually a good choice. Most of what is said in articles about Java exceptions is applicable to Apex exceptions. And don’t forget about try/finally.)

But when a test is deliberately provoking an exception and you want to check the exception, then a try/catch in the test is needed. A common case of this is where you are testing that a trigger is using addError to report an error back to a user:

try {
    System.assert(false, 'Exception expected');
} catch (DMLException e) {
    System.assert(e.getMessage().contains('expected message'), 'message=' + e.getMessage());

Note the test should fail if the exception isn’t thrown, hence the System.assert(false, 'Exception expected');.

DMLException has extra methods that allow you to examine in more detail what is being reported in a test if the extra information is important.

Source : Link , Question Author : Bforce , Answer Author : Keith C

Leave a Comment