0

Any ideas how I can test this one - only the DMLException and Exception parts

@future
public static void updateUserFlags(Set<Id> userIds ) {

    Logger.log(ShGl_Constants.INFO, 'ShMa_AveretekOptInUser.updateUserFlags', 'Start user update', null);
    // Create empty map for user updates
    Map<Id, User> userUpdateMap = new Map<Id, User>();

    // Iterate users for ids passed into future method
    for (User u2 : [SELECT Id, ShMa_OptInAveretek__c,
                    Is_Integrated__c, Contact.ShMa_OptInAveretek__c FROM User WHERE Id in: userIds]) {

        // Set user fields
        User u3 = new User( Id = u2.Id, Is_Integrated__c = false,
                            ShMa_OptInAveretek__c = u2.Contact.ShMa_OptInAveretek__c);
        userUpdateMap.put(u2.Id, u3); // add intsance of user to map
    }


    try {
        if (userUpdateMap.size() > 0) {
            update userUpdateMap.values(); // update user records in map
            Logger.log(ShGl_Constants.INFO, 'ShMa_AveretekOptInUser.updateUserFlags', 'End user update', null);
        }
    }

    // Start exception handing
    catch (DmlException e) {
        for (Integer i = 0; i < e.getNumDml(); i++) {
            Logger.log(ShGl_Constants.ERROR, 'ShMa_AveretekOptInUser.updateUserFlags', e.getDmlMessage(i), e.getDmlId(i));
        }
    } catch (Exception e) {
        Logger.log(ShGl_Constants.FATAL, 'ShMa_AveretekOptInUser.updateUserFlags', e.getMessage(), null);
    }

}
4
  • 2
    Odds are, you can't test this intentionally without sabotaging your code; it's fairly bulletproof as it is. I've said this more than once here, but I'll repeat it again-- 100% coverage should be impossible if you have good error handling routines.
    – sfdcfox
    Commented Jan 12, 2015 at 23:31
  • thanks foxy! It's very annoying though, means I can never get above 80% coverage. I was hope by now there was a way. I guess I could remove the fls and try to kill that way. Commented Jan 12, 2015 at 23:36
  • Don't do it. You're not meant to get 100% coverage. Code for performance, and let the coverage be Just Good Enough. The Pareto Principle is in play, and you don't want to be its victim. I've gone down that road before.
    – sfdcfox
    Commented Jan 12, 2015 at 23:42
  • Finally, I found what I was looking for: read this.
    – sfdcfox
    Commented Jan 12, 2015 at 23:44

1 Answer 1

1

Move it to a separate method that takes in the list you want to update.

static void updateUsers(List<User> usersToUpdate)
{
    try
    {
        update usersToUpdate;
    }
    catch (DmlException dmx)
    {
        //do stuff
    }
}

And your test class:

@TestVisible
static testMethod void testUpdateUsers_Error()
{
    // no Id causes DmlException on update
    List<User> errorUsers = new List<User> { new User(); }

    DmlException unexpectedException;
    Test.startTest();
        try
        {
            updateUsers(errorUsers);
        }
        catch (DmlException dmx)
        {
            unexpectedException = dmx;
        }
    Test.stopTest();

    system.assertEquals(null, unexpectedException, 'Add a message');
    //additional assertions...
}

You must log in to answer this question.

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