3

NOTE : This question is not duplicate.Please go through code once.

Here is the situation -

I am calling this method from my test class, and here My_obj__c records are getting created and added in list, which list I insert later in "TRY" block (so point is I am not able to generate an exception-by making list empty or something like that from test Class)

private boolean mymethod(){
      My_obj__c obj = new My_obj__c(name='test');

      List<My_obj__c> lst = new List<My_obj__c>();
      lst.add(obj);

try{
    insert lst;
   }
   catch(Exception e){
         /////////// 15 lines code --- need to cover
   }
}

Any Solution, suggestions, help. Please reply.

3
  • 1
    Is it catch (DmlException e) or catch (Exception e)?
    – Keith C
    Nov 26, 2015 at 15:28
  • 1
    Here's what I would ask, if you can't manually generate an error then what type of error are you expecting to find once this is working? Nov 26, 2015 at 17:49
  • @KeithC - Its catch(Exception e){ } Nov 27, 2015 at 12:18

3 Answers 3

3

You could add this to the code:

try {
    insert lst;
    TestOptions.throwDmlException();
} catch(Exception e){
    ...
}

where the added class is (as a DmlException can be constructed):

public class TestOptions {
    public static Boolean throwDmlException = false;
    public static void throwDmlException() {
        if (throwDmlException) throw new DmlException();
    }
}

and then in your test case turn the option on where needed:

TestOptions.throwDmlException = true;
2
  • This looks great, but one more thing - Can we do it without updating/chaning my Class code ? Nov 27, 2015 at 12:23
  • 1
    @NileshMendhe No. This approach requires a change to the code you are testing.
    – Keith C
    Nov 27, 2015 at 12:59
0

You can add a empty object which will give exception while insertion

 My_obj__c obj = new My_obj__c();

Also, as mentioned in above comment by Keith which exception you are catching catch (DmlException e) or catch (Exception e)?

try{
    insert lst;
   }
   catch(Exception e){
         /////////// 15 lines code --- need to cover
   }
}
2
  • catch(Exception e) Nov 27, 2015 at 12:21
  • No, I cant change the Class functionality by adding Empty object. I have to do it from TestClass ORRRRR if no then not changing any Logic of Class/Controller which is already working fine Nov 27, 2015 at 12:23
0
 List<My_obj__c> lst = new List<My_obj__c>();
      lst.add(obj);

try{
    insert lst;
     if(Test.isRunning)
    {
      Integer i = 1/0;
     }
   }
   catch(Exception e){
         /////////// 15 lines code --- need to cover
   }
}

Please note the point mentioned by @cropredy below

small issue here -- the exception will be thrown for all testmethods - including ones that need to legitimately and successfully insert my_obj__c. You need to have the testmethod signal the try block that it wants to have an exception thrown. Pravesh has a better solution for triggering a DML exception - omit a required field

1
  • 2
    small issue here -- the exception will be thrown for all testmethods - including ones that need to legitimately and successfully insert my_obj__c. You need to have the testmethod signal the try block that it wants to have an exception thrown. Pravesh has a better solution for triggering a DML exception - omit a required field
    – cropredy
    Nov 26, 2015 at 16:44

You must log in to answer this question.

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