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?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
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:
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
Note the test should fail if the exception isn't thrown, hence the
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
Then your test class would include code to cause this error, and would include a try catch like this
In my opinion in cases like this you should use the ExpectedExceptionAttribute Class