Below is my batch apex class that updates Account object for example:

global class abc implements Database.Batchable<sObject>{

global Database.QueryLocator start(Database.BatchableContext batchableContext ) {
 String query = 'SELECT Id,Name,IsActive FROM Account';

    global void execute(Database.BatchableContext batchableContext, List<User> scope) {
      // logic to jus update IsActive field to true or false
// Create a Map<Id,List<Account>> newAccountMap;
  Database.SaveResult[] updateResult = Database.update(newAccountMap.values(), false);

            for(Integer i=0; i<updateResultList.size(); i++) {
                Database.SaveResult sr = updateResultList.get(i);
                if (sr.isSuccess()) {
                    // logs a message
                } else {
                   // unable to reach here via test method.
                   // logs the error message here

    global void finish(Database.BatchableContext batchableContext) {



Test method

public static void testBatchClass{

            abc batcObj = new abc();
            Id batchId = Database.executeBatch(batcObj);


How to I cause a DML exception in this case to test the part where I log the error messages?

  • do you have acccount trigger? Feb 25, 2020 at 21:37
  • I took account as an example. But the actual object is User and it doesnt have a trigger
    – SfdcBat
    Feb 25, 2020 at 21:39
  • Take a look here: salesforce.stackexchange.com/questions/254823/…
    – Rodrigo
    Feb 25, 2020 at 21:42
  • this may not be helpful as my logic is in execute method. I have simplified my code to look like that but there are some processing happening in there. Also I return a log record that I jus insert in finish method. In other advice?
    – SfdcBat
    Feb 25, 2020 at 21:49
  • Create account records that would trip up a particular error, like an invalid value in a restricted picklist field or trying to update a field that's uneditable, etc Feb 25, 2020 at 22:01

1 Answer 1


The first question you need to ask is

Is it even worth using the partial success pattern for something as simple as flipping an IsActive flag on User

If yes, then the next question worth asking,

is it truly important to cover the error condition arising from partial success?

As others have pointed out on SFSE time and again, it can be a fool's errand trying to get code coverage over exceptions that are difficult to generate.

So, if you still want to test out partial successes, you will need to change your batchable to accept dependency injection via a constructor arg

Such constructor arg might be a map of values by fieldName so you can have the code under test use that map (default would be empty) to set the values of the Account object before updating.

For example, the testmethod could define:

map<SobjectField, Object> valuesByField = new Map<SObjectField,Object> {
 Account.Name => null
 Account.IsActive => true

and then pass to the batchable's constructor and the execute() would use that map to coerce values before doing the update. An account with a null Name will fail as it is a required field.

Sometimes you need to design the application to be testable in a way that mocks can be easily used. Enterprise Patterns on Trailhead is one architecture to consider. I use that all the time to mock exceptions in services without ever having to create DML data.

  • Yes, i have used mocks in the past and that is what i was exactly thinking. However i jus simplified the whole class that i am working on ( tech debt ) to ask as a question. There is whole lot of crap written and untangling it would require lot of work and lot of regression testing. Guess wat the existing test class does not do anything other than code coverage. Lol. Your point is very well taken i will have to design the whole thing. Thanks!!
    – SfdcBat
    Feb 25, 2020 at 23:36
  • 1
    +1 for there is a whole lot of crap...
    – cropredy
    Feb 26, 2020 at 1:53

You must log in to answer this question.

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