5

Wrote a test class for a class which code coverage is 86%. How do I test for an exception('addError')?

Have a look

 for (Account_Affiliation__c a: c.R00N300000014qT8EAI__r) {
    if (a.recordtypeid == rtid && a.Status__c == 'ACTIVE' && a.Primary__c == true) {
        if (id1 != null) { * *
            if (orgmap.keyset().contains(a.id))
                orgmap.get(a.id).addError('A contact should not be allowed to have more than 1 “Primary”, “Active”, “Employment” record at the same time. '); * *
        }
        id1 = a.Related_Account__c;
    }
    if (a.recordtypeid == rtIdemp && a.Status__c == 'ACTIVE' && a.Primary__c == true && a.Type__c == 'College') {

        if (id2 != null) { * *
            if (orgmap.keyset().contains(a.id))
                orgmap.get(a.id).addError('- A contact should not be allowed to have more than 1 “Primary”, “Active”, “Education”, “College” record at the same time. '); * *


        }
        id2 = a.Related_Account__c;
    }
    if (a.recordtypeid == rtIdemp && a.Status__c == 'ACTIVE' && a.Primary__c == true && (a.Type__c == 'Grad School (Non-MBA)' || a.Type__c == 'MBA')) {

        if (id3 != null) { * *
            if (orgmap.keyset().contains(a.id))
                orgmap.get(a.id).addError('- A contact should not be allowed to have more than 1 “Primary”, “Active”, “Education”, “Grad School (Non-MBA)” OR “MBA” record at the same time'); * *


        }
        id3 = a.Related_Account__c;
    }

}
7

To cover all the error cases you will have to create records that match each of the if conditions in your code. I suggest a separate test method for each one.

To actually test the code rather than just increase the coverage, you should assert the expected behaviour i.e. that the error has been added. In your test code the error will come through as an exception. So you need to fail the test if the exception doesn't happen and also assert that the expected error message is generated:

try {
    insert a;
    System.assert(false, 'exception expected');
} catch (DmlException e) {
    String message = e.getMessage();
    System.assert(message.contains('expected error message'), 'message=' + message);
}
4

If you insert a list and want to check for every message:

    Test.startTest();
    try {
        insert myList;
        System.assert(false, 'DmlException expected!');
    } catch(System.DmlException x) {
        for(Integer i = 0; i < myList.size(); i++)
            System.assertEquals(x.getDmlMessage(i), 'My error message');
    }
    Test.stopTest();

As in the Apex documentation: https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex7_3.htm

2

You should insert another Account_Affiliation__c record of same properties in your if condition to get the error(looking from your partial code).

Account_Affiliation__c a1 = new Account_Affiliation__c(recordtypeid = 'xxxxxxxxxxx',Status__c = 'ACTIVE',Primary__c = true);
insert a1;

Account_Affiliation__c a2 = new Account_Affiliation__c(recordtypeid = 'xxxxxxxxxxx',Status__c = 'ACTIVE',Primary__c = true);
try{
    insert a2;
}
catch(Exception e){}

The reason for surrounding second insert with try catch block is that it will return exception thrown using addError, so the test will fail.

Hope it helps.

  • Thanks Vigneshwaran G for prompt reply. I had used try catch condition on DML. but still code coverage 86%. – sanjeev kaurav Jul 20 '15 at 8:19
  • You should insert two Account_Affiliation__c records with almost same properties to invoke addError in your controller. It will cover those lines even if you don't use try catch in your test method. But the test method will fail. – Vigneshwaran G Jul 20 '15 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.