I'm trying to write a test class to cover the try/catch section in the following code but am unsure how to test for the error. I only have 69% code coverage w/o it.
Or is there a way in the test class to force the EmailFailure string to not be null so I can get coverage on that section?
trigger Events_SummerCamp_AutoEmail on CnP_PaaS_EVT__Event_registrant_session__c (before insert, before update) {
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
List<ID> RequireContactId = new List<ID>();
List<ID> RequireEventId = new List<ID>();
for(CnP_PaaS_EVT__Event_registrant_session__c CP : trigger.new)
{
if(CP.CnP_PaaS_EVT__ContactId__c != null && CP.CnP_PaaS_EVT__EventId__c != null){
RequireContactId.add(CP.CnP_PaaS_EVT__ContactId__c);
RequireEventId.add(CP.CnP_PaaS_EVT__EventId__c);
}
}
Map<ID,Contact> ContactData = new Map<Id,Contact>([Select Owner.Email, OwnerId, Email, Name, ID from Contact where id in : RequireContactId limit 1000 ]);
Map<ID,CnP_PaaS_EVT__Event__c> EventData = new Map<Id,CnP_PaaS_EVT__Event__c>([Select ID, Name from CnP_PaaS_EVT__Event__c where ID in : RequireEventId limit 1000 ]);
for(CnP_PaaS_EVT__Event_registrant_session__c newCP: trigger.new){
if (newCP.CnP_PaaS_EVT__ContactId__c != NULL && newCP.CnP_PaaS_EVT__EventId__c != NULL && newCP.Email_Sent__c == FALSE){
Contact theContact = ContactData.get(newCP.CnP_PaaS_EVT__ContactId__c);
CnP_PaaS_EVT__Event__c theEvent = EventData.get(newCP.CnP_PaaS_EVT__EventId__c);
String EventName = theEvent.Name;
if (EventName.Contains('Summer Camp')== TRUE){
if (theContact.Email != NULL){
User uEmail = [select Email from User where Id = :newCP.OwnerID limit 1];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = :uEmail.Email limit 1];
Messaging.SingleEmailMessage sendEmail = new Messaging.SingleEmailMessage();
//Summer Camp Email
sendEmail.setTemplateID('00Xd0000001EkoC');
sendEmail.setTargetObjectId(theContact.Id);
sendEmail.setWhatId(newCP.id);
if ( owea.size() > 0 ) {
sendEmail.setOrgWideEmailAddressId(owea.get(0).Id);
}else{
sendEmail.setOrgWideEmailAddressId('0D2d0000000CbZv');
}
emails.add(sendEmail);
newCP.Email_Sent__c = TRUE;
}
}
}
}
String EmailFailure = null;
try {
Messaging.SendEmailResult[] mailResult = Messaging.sendEmail(emails);
if ( !mailResult[0].isSuccess() ) {
EmailFailure = 'Email Failed: ' + mailResult[0].getErrors()[0].getMessage();
}
}
catch(DmlException dmlEx) {
EmailFailure = 'Email Failed: ' + dmlEx;
}
catch(System.EmailException emlEx) {
EmailFailure = 'Email Failed: ' + emlEx;
}
if ( EmailFailure != null ) {
Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'[email protected]'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('[email protected]');
mail.setSenderDisplayName('Apex error message');
mail.setSubject('Summer Camp Error');
mail.setPlainTextBody(EmailFailure);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Test Class:
@isTest
public class Events_SummerCamp_Email_Test{
static testmethod void test_SC(){
Contact c = new Contact(
LastName = 'Testclass',
RecordTypeId = '012d0000000syet',
Email = '[email protected]'
);
insert c;
CnP_PaaS_EVT__Event__c e1 = new CnP_PaaS_EVT__Event__c(
Name = '2014 Summer Camp',
CnP_PaaS_EVT__Event_title__c = 'Summer Camp 2014',
CnP_PaaS_EVT__Site_Url__c = 'https://ecad1.secure.force.com/Events',
CnP_PaaS_EVT__Start_date_and_time__c = system.today(),
CnP_PaaS_EVT__End_date_and_time__c = system.today(),
CnP_PaaS_EVT__Publish_date__c = system.today()
);
insert e1;
CnP_PaaS_EVT__Event__c e = [select id from CnP_PaaS_EVT__Event__c where Name LIKE '%Summer Camp%' limit 1 ];
CnP_PaaS_EVT__Event_registrant_session__c r = new CnP_PaaS_EVT__Event_registrant_session__c(
Name = 'Testclass',
CnP_PaaS_EVT__ContactId__c = c.Id,
CnP_PaaS_EVT__EventId__c = e.Id
);
insert r;
CnP_PaaS_EVT__Event_registrant_session__c r2 = [select id,Email_Sent__c from CnP_PaaS_EVT__Event_registrant_session__c where id = :r.id limit 1 ];
System.assertEquals(TRUE, r2.Email_Sent__c);
}
}
EmailException
isn't the most logical approach...what if the exception was that you've exhausted your daily limit? Also worth noting that sending emails to aUser
usingTargetObjectId
does not consume that daily limit, whereas any usage oftoAddresses
does.