12

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?

3 Answers 3

20

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.

1
  • 1
    the additional notes which you gave is good to understand the concept better.
    – Bforce
    Commented Mar 27, 2014 at 9:15
7

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());
}
0
-3

In my opinion in cases like this you should use the ExpectedExceptionAttribute Class

example [TestClass()]

 public class DivisionClassTest
    {
        [TestMethod()]
        [ExpectedException(typeof(System.DivideByZeroException))]
        public void DivideTest()
        {
            DivisionClass target = new DivisionClass();
            int numerator = 4;
            int denominator = 0;
            int actual;
            actual = target.Divide(numerator, denominator);
        }
    }
2
  • Welcome to SFSE! Commented Mar 27, 2014 at 20:51
  • 5
    ... remember this is Apex, Salesforce's proprietary language, which doesn't have that capability.
    – Keith C
    Commented Mar 27, 2014 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .