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

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.


Yes you can use try catch inside a test class, and this is how you would test a class that you have written that can throw an exception

For example if your class included something like this

if (bSomeTestCondition == true) {
   // success code
} else {
   Exception e = new myClassException();
   e.setMessage('Incorrect bSomeTestCondition');
   throw e;
}

Then your test class would include code to cause this error, and would include a try catch like this

try {
    Test.startTest();
    //Call your class in a way that causes the exception
    Test.stopTest();
} catch (myClass.myClassException e) {
    System.assertEquals('Incorrect bSomeTestCondition', e.getMessage());
}

Tags:

Unit Test