0

Is there a way to cover the following catch block without of course doing test.isRunning.

Try{
     Id bId = Func1();
     Schema.DescribeSObjectResult result = Obj1.SObjectType.getDescribe(); 
     pageRef = new PageReference('/' + result.getKeyPrefix()); 
     pageRef.setRedirect(true); 
}
catch{
    // blah blah blah
}

Func1:

public id Func1(){
    Id batchProcessId; 
    ClassA BatchX = new ClassA(); 
    batchProcessId = Database.executeBatch(BatchX); 
    return batchProcessId;
}
4
  • what is Func1? Can we simulate exception inside of Func1 ?
    – Patlatus
    Commented Dec 14, 2016 at 12:07
  • Func1: public id Func1() { Id batchProcessId; ClassA BatchX = new ClassA(); batchProcessId = Database.executeBatch(BatchX); return batchProcessId; }
    – starhunter
    Commented Dec 14, 2016 at 12:24
  • Do you have any code inside ClassA constructor to simulate exception inside of new ClassA() ?
    – Patlatus
    Commented Dec 14, 2016 at 12:29
  • I don't know how can that be done when we are not passing any parameter in the constructor. ---------ClassA BatchX = new ClassA();--------
    – starhunter
    Commented Dec 14, 2016 at 12:54

1 Answer 1

1

I think you could try some approach like this

public class ClassA{
@testVisible static Boolean raiseException{ get{ if ( raiseException == null ) raiseException = false; return raiseException; } set; }

public ClassA(
    if (raiseException)  throw SomeFakeException();
)

}

Then in test

testMethod static void coverCatchBlock() {
   ClassA.raiseException = true;

   // perform test
}

You must log in to answer this question.

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