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
StaciStaci 

test coverage for email attachments

Here's my email handler
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;


 
Best Answer chosen by Staci
Bryan Leaman 6Bryan Leaman 6
You need to add an attachment to your test email message. Here's a sample:
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
email.subject = 'ref';
email.PlainTextBody = 'test body';

// Add an Binary attachment to the email message!
Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
attachment.body = blob.valueOf('my attachment content');
attachment.fileName = 'filename.txt';
attachment.mimeTypeSubType = 'text/plain';
email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };

DSSEmailHandler m = new DSSEmailHandler();
m.handleInboundEmail(email, env);