1

My actual method is something like :

public static string Method(String input ) {
    String Output;
    try {
        Output = StringFunction(Input);
    } catch(Exception e) {}
    return output;
}

Test method written so far :

public static void testMethod() {
    String testInput = 'SomeString';
    String testOutput ;
    try {
        testOutput = Class.StringFunction(testInput);
    } catch(Exception e) {}
}

Currently the exception is not being covered . How can I cover the exception ?

3
  • Why do you need to do this? Only way to do this properly is to force error in StringFunction - and unless this is possible you can't cover exception Commented Nov 23, 2016 at 9:01
  • 1
    @ArturKępczyński- As per client requirements , I need to add both correct value as well as exception.
    – SFDCRookie
    Commented Nov 23, 2016 at 9:12
  • Then you either need to find scenario when StringFunction wil lthrow exception or implement mechanism to cause such exception in test (which is stiupid ) Commented Nov 23, 2016 at 9:18

3 Answers 3

3

I used to do something like the code below in the main Method code

if(Test.isRunningTest() && otherTestScenarioCondition) throw new MyException();
2

Pass any incorrect parameter to your StringFunction that should do the job for you!

0

You could also try the following:

 if(Test.isRunningTest()) {
            DmlException e = new DmlException();
            e.setMessage('This is a constructed exception for testing and code coverage');
            throw e;
        }

You must log in to answer this question.

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