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;
}
  • what is Func1? Can we simulate exception inside of Func1 ? – Patlatus Dec 14 '16 at 12:07
  • Func1: public id Func1() { Id batchProcessId; ClassA BatchX = new ClassA(); batchProcessId = Database.executeBatch(BatchX); return batchProcessId; } – starhunter Dec 14 '16 at 12:24
  • Do you have any code inside ClassA constructor to simulate exception inside of new ClassA() ? – Patlatus Dec 14 '16 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 Dec 14 '16 at 12:54
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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.