Salesforce 无法在单元测试中捕获DmlException

Salesforce 无法在单元测试中捕获DmlException,salesforce,apex-code,Salesforce,Apex Code,我试图在catch块中测试DmlException,但无法在单元测试中抛出DmlException错误 我正在测试的代码将联系人所有者字段更新为相关的帐户所有者。代码如下: global class AccountUpdateContactOwnership implements Triggers.HandlerInterface { Account[] newCollection = trigger.new; Map<Id, Account> oldCollection = (M

我试图在catch块中测试DmlException,但无法在单元测试中抛出DmlException错误

我正在测试的代码将联系人所有者字段更新为相关的帐户所有者。代码如下:

global class AccountUpdateContactOwnership implements Triggers.HandlerInterface {

Account[] newCollection = trigger.new;
Map<Id, Account> oldCollection = (Map<Id, Account>)trigger.oldMap;
Set<Id> accountIds = new Set<Id>();

global void handle() {
    Map<Id, List<Contact>> accountToContacts = getAllContactsByAccount(getAccountIds());
    updateContactOwnershipWhenAccountOwnershipChanges(accountToContacts);
}

private Set<Id> getAccountIds() {
    for(Account a : newCollection) {
        if(a.ownerId != oldCollection.get(a.Id).ownerId) accountIds.add(a.Id);
    }
    return accountIds;
}

private Map<Id, List<Contact>> getAllContactsByAccount(Set<Id> accountIds) {
    Map<Id, List<Contact>> accountToContacts = new Map<Id, List<Contact>>();
    if(!accountIds.isEmpty()) {
        List<Contact> listContacts = new List<Contact>();
        Map<Id, Contact> mapContact = new Map<Id, Contact>([select Id, AccountId, OwnerId From Contact Where accountId in: accountIds]);

        for(Contact c : mapContact.values()) {
            listContacts = accountToContacts.get(c.AccountId);
            if(listContacts == null) {
                listContacts = new List<Contact>();
                accountToContacts.put(c.accountId, listContacts);
            }
            listContacts.add(c);
        }
    }
    return accountToContacts;
}

private void updateContactOwnershipWhenAccountOwnershipChanges(Map<Id, List<Contact>> accountToContacts) {  
    if(!accountToContacts.isEmpty() && accountToContacts.size() > 0) {  
        List<Contact> contactsToUpdate = new List<Contact>();

        for(Account a : newCollection) {
            List<Contact> contacts = accountToContacts.get(a.Id);
            if(!contacts.isEmpty() && contacts.size() > 0) {
                for(Contact c : contacts) {
                    c.OwnerId = a.OwnerId;
                    contactsToUpdate.add(c);
                }
            }           
        }

        if(!contactsToUpdate.isEmpty() && contactsToUpdate.size() > 0) {
            try {
                update contactsToUpdate;
            }
            catch(DmlException e) {
                for(integer i=0;i<e.getNumDml();i++) {
                    System.debug(e.getDmlMessage(i));
                }
            }
        }
    }       
}

}
initialize()设置数据(帐户和联系人)。我认为将帐户所有者设置为联系人会抛出DmlException,但它仍然可以处理,并且不会进入catch块。我还尝试将帐户所有者设置为null,但也不起作用。我如何强制使用DmlException?在这种情况下,我是否不需要尝试捕捉DmlException?我已经阅读了大量关于在Salesforce中测试异常的困难的帖子,但还没有找到解决方案


有人能帮忙吗?

我想您在测试中没有出现异常,因为您在触发器的代码中处理了它(第52-59行)。您不需要测试带有dml异常的否定情况,因为您在触发器的代码上处理它

static testMethod void accountUpdateContactOwnershipNegativeTest() {
    initialize();

    System.Dmlexception ex;

    test.startTest();   
        try {
            testAccount.OwnerId = testContact1.Id;
            update testAccount;
        }
        catch(DmlException e) {
            ex = e;
        }
    test.stopTest();

}