You need to sign in to do that
Don't have an account?

test coverage for email attachments
Here's my email handler
I can't get this part covered, I'm at 70% without it
global class DSSEmailHandler implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); if(email.subject.contains('ref')) { try { Case c = new Case(); c.ContactId = email.fromAddress; c.recordTypeId = '0121O000001WL0u'; c.OwnerId = '00G1O000004lFB4'; c.Description = email.plainTextbody; c.Subject = email.subject; insert c; //to save attachment to case for(Messaging.Inboundemail.BinaryAttachment bAttachment: email.binaryAttachments) { Attachment attachment = new Attachment(); attachment.Name = bAttachment.fileName; attachment.Body = bAttachment.body; attachment.ParentId = c.Id; insert attachment; } result.success = true; }catch (Exception e){ result.success = false; result.message = 'Oops, I failed.' + e.getMessage(); } return result; } return null; } }Here's the test code
@isTest private class DSSEmailHandlerTest { public static Blob createAttachmentBody(){ String body = 'XXXXXXXXXXXXX'; return Blob.valueof(body); } public static testMethod void testmyHandlerTest() { Case c = new Case(); insert c; //String cName = [SELECT Name FROM Case WHERE Id = :c.Id LIMIT 1].Name; Messaging.InboundEmail email = new Messaging.InboundEmail(); Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); email.subject = 'ref'; email.plainTextBody = 'test body'; DSSEmailHandler m = new DSSEmailHandler(); m.handleInboundEmail(email,env); // attachment insert Attachment att = new Attachment(Name = 'XXXXXXXX', Body = Blob.valueof('XXXXXXXXXXXXXX'), ParentId = c.Id); insert att; } }
I can't get this part covered, I'm at 70% without it
Attachment attachment = new Attachment(); attachment.Name = bAttachment.fileName; attachment.Body = bAttachment.body; attachment.ParentId = c.Id; insert attachment; } result.success = true;

You need to add an attachment to your test email message. Here's a sample: