0

How can we throw DML exceptions in Batch Start method in apex? I want to cover a code using test class which handles dml exception in select query in batch start method. Query is simple select query

global database.querylocator start(Database.BatchableContext bc){
      try{
      return Database.getQueryLocator('Select Id,Account_Unique__c from Account');
      }catch(Exception e){
     string logId= CustomExceptionController.CreateLog(null,e) ;
     return null;
     }
}
1
  • Can you please add the code that you are intent to use for this? Dec 6, 2018 at 10:51

3 Answers 3

2

You seem to be misinterpreting few things here.

I want to cover a code using test class which handles dml exception in select query in batch start method.

A SELECT ... query never throws a DMLException. DMLException, as the name suggests is thrown only on DML Operations. Refer to the description as below from the documentation for Exceptions. Also refer to the documentation which specifies what constitutes DML Operations in Apex.

Any problem with a DML statement, such as an insert statement missing a required field on a record.

In your current form of the code, the execution flow will never reach the Exception block. So instead of attempting to cover these lines in your test class, you should instead remove the Exception block in your batch class, which is not required.

0

You can do something in this lines:

}catch(DMLException e){
            System.debug('The following exception has occurred when inserting contacts records: ' + e.getMessage());
            System.Assert(e.getMessage().contains('Required fields are missing'), 'Update did not fire');
        }

If you now the error you just add the e.getMessage().contains('Part of error').

So to test this you need to create a method in your test class that will insert negative parameters or missing data for mandatory fields, something that will trigger the error. You can then in you Developer Console catch the error and use the message on that error to populate your assert.

Check this example from on of my test classes:

@isTest static void negtiveInsertion() 
    {

        // Getting test data 
        List<Contact> contacts = Contact_TestFactory.contactsNegativeInput(200);
        Database.DMLOptions dml = new Database.DMLOptions();
        dml.DuplicateRuleHeader.allowSave = true;
        dml.DuplicateRuleHeader.runAsCurrentUser = false;

        // Inserting contacts with Want_to_heard_more__c set to true
        Test.startTest();

        try{
            database.insert(contacts, dml);
        }catch(DMLException e){
            System.debug('The following exception has occurred when inserting contacts records: ' + e.getMessage());
            System.Assert(e.getMessage().contains('Required fields are missing'), 'Update did not fire');
        }
        Test.stopTest();

        // Runing asserts to control insert
        System.assertEquals(200, contacts.size()); 



    }
4
  • I want to throw an error in select query and not in insert query Dec 6, 2018 at 11:01
  • 1
    Umm, SELECT is not a DML operation. Can you elaborate a bit more on this case? Dec 6, 2018 at 11:03
  • I want to throw an exception not necessarily dml which runs when I execute select query inside Batch start method Dec 6, 2018 at 11:04
  • You want to throw and error when a query is been executed? Is this what you are asking? By "execute". what it is that you mean? Returning results from that query? What criteria is the one you are intending to preform here? Could you tell us the value of doing such error handling operation. Please, give more details in order to understand what it is that you are trying to accomplish. It looks like you are also using Custom Exception structure... will be nice to see and understand this part too. Dec 6, 2018 at 12:13
0

You cannot contrive to make this code throw an exception because there isn't a catchable exception that would be thrown here.

try {
    return Database.getQueryLocator('Select Id,Account_Unique__c from Account');
} catch(Exception e) {
    String logId = CustomExceptionController.CreateLog(null,e) ;
    return null;
}

You should simply remove the exception handler.

return Database.getQueryLocator('Select Id,Account_Unique__c from Account');

You must log in to answer this question.

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