function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlphaPAlphaP 

Test Error Method

I have 90% test class coverage for a trigger with the exception of this one line in my code:

 

 27    else if ((tmpDNCList.size()<1) && (myLead.Sub_Status_ALL__c == 'DNC Requested'))
 28   {
 29   Trigger.new[0].Status.addError('Please add DNC record to Salesforce prior to saving.');

 

 

When I create a record in the test class that matches this critieria - it causes a DML exception (as it should) and halts all of my other tests if I run them all at once and it drops my code coverage to almost 50%.   The class itself gets 100% coverage though.  

 

Is there something sepcial about wiritng a test class when using the error method?

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

in your test code,

 

put the dml in a try catch block catching dml exceptions....

 

Try{

 

     insert xxx;

}catch (dmlException e){

    system.assertEquals(True,e.getDMLMessage(0).contains('Please add DNC record to Salesforce prior to saving.'));

}

All Answers

Starz26Starz26

in your test code,

 

put the dml in a try catch block catching dml exceptions....

 

Try{

 

     insert xxx;

}catch (dmlException e){

    system.assertEquals(True,e.getDMLMessage(0).contains('Please add DNC record to Salesforce prior to saving.'));

}

This was selected as the best answer
AlphaPAlphaP

Thanks - I didn't even know what to search in the dev guides.    Exactly what I needed.