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
Shawn Reichner 33Shawn Reichner 33 

Help with Test Class for Apex Incoming Email Service where Custom Object is Created

Hello awesome developers! 

I have the following Apex class that implements an incoming Apex Email Service.  The class works 100% with no issues and I get a Custom Object record creatd and all is good.  The issue is when I attempted to create a test class for this service.  I get the lines of the email service covered but the record being created is not being covered.  This is due to in my test class below you will notice the SOQL query to bring back the custom object record that was created by the test class to assert against and there are no rows for assignment.  

Can you kindly help me update the beloe test class to cover 100% and to cover the custom object record that is being created. 

Thank you so much in advance for any help you are able to provide,

Shawn

Class:
global class SFDCIssueEmailClass implements Messaging.InboundEmailHandler {
 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                       Messaging.InboundEnvelope env){
 
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
  
    String myPlainText= '';
    Group queue = [SELECT Id FROM Group WHERE Name = 'Salesforce Queue' and Type='Queue'];
                                                           
    myPlainText = email.plainTextBody;
   
    Salesforce_Issue__c[] newTask = new Salesforce_Issue__c[0];
   
    try {
      User vCon = [SELECT Id, FirstName, LastName, Email
        FROM User
        WHERE Email = :email.fromAddress
        LIMIT 1];
      
      newTask.add(new Salesforce_Issue__c(Descripton__c =  myPlainText,
           Priority__c = 'P4',
           Status__c = 'New',
           Subject__c = email.subject,
           Email_Requestor__c =  email.fromAddress,
           OwnerId = queue.Id,
           Origin__c = 'Email',
           RecordTypeId = '012q00000005mXhAAI'));
     

     insert newTask;    
     
     System.debug('New Task Object: ' + newTask );   
    }

   catch (QueryException e) {
       System.debug('Query Issue: ' + e);
   }

   result.success = true;
   
   return result;
  }
}

Test Class:
@isTest
public class SFDCIssueEmailClassTest {

    public static testMethod void EmailTest(){
        
        Contact c = new Contact(lastname = 'tested', email = 'test@test.com');
        insert c;
        
        SFDCIssueEmailClass objconfirm = new SFDCIssueEmailClass();
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        email.fromAddress = c.email;
        email.subject = 'Test Subject';
        email.plainTextBody = 'my text';
          
       // objconfirm.handleInboundEmail(email, envelope);
        Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
        objconfirm.handleInboundEmail(email, envelope);
        Messaging.InboundEmailResult result = objconfirm.handleInboundEmail(email, envelope);
        system.assertEquals(result.success, true); 
        
        Salesforce_Issue__c[] issue = [SELECT ID FROM Salesforce_Issue__c WHERE Email_Requestor__c = :email.fromAddress];
        system.assertEquals(1, issue.size(),'Issue not inserted');
        
    }
    
}

​​​​​​​
Best Answer chosen by Shawn Reichner 33
David Zhu 🔥David Zhu 🔥
You will need to use running as a user otherwise soql on user will fail since email.address won't find a match user.
Try use the following sample as a reference.
public static testMethod void EmailTest(){
        

        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];  
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com',

        Contact c = new Contact(lastname = 'tested', email = 'test@test.com');
        insert c;
        
        System.runAs(u) {

        SFDCIssueEmailClass objconfirm = new SFDCIssueEmailClass();
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        email.fromAddress = c.email;
        email.subject = 'Test Subject';
        email.plainTextBody = 'my text';
          
       // objconfirm.handleInboundEmail(email, envelope);
        Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
        objconfirm.handleInboundEmail(email, envelope);
        Messaging.InboundEmailResult result = objconfirm.handleInboundEmail(email, envelope);
        system.assertEquals(result.success, true); 
        
        Salesforce_Issue__c[] issue = [SELECT ID FROM Salesforce_Issue__c WHERE Email_Requestor__c = :email.fromAddress];
        system.assertEquals(1, issue.size(),'Issue not inserted');
        
       }
  }

 

All Answers

Shawn Reichner 33Shawn Reichner 33
Lines 21 through 31 are the lines in the Apex Class that are not being covered in the test class.  Thanks again for your help!
David Zhu 🔥David Zhu 🔥
You will need to use running as a user otherwise soql on user will fail since email.address won't find a match user.
Try use the following sample as a reference.
public static testMethod void EmailTest(){
        

        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];  
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com',

        Contact c = new Contact(lastname = 'tested', email = 'test@test.com');
        insert c;
        
        System.runAs(u) {

        SFDCIssueEmailClass objconfirm = new SFDCIssueEmailClass();
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        email.fromAddress = c.email;
        email.subject = 'Test Subject';
        email.plainTextBody = 'my text';
          
       // objconfirm.handleInboundEmail(email, envelope);
        Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
        objconfirm.handleInboundEmail(email, envelope);
        Messaging.InboundEmailResult result = objconfirm.handleInboundEmail(email, envelope);
        system.assertEquals(result.success, true); 
        
        Salesforce_Issue__c[] issue = [SELECT ID FROM Salesforce_Issue__c WHERE Email_Requestor__c = :email.fromAddress];
        system.assertEquals(1, issue.size(),'Issue not inserted');
        
       }
  }

 
This was selected as the best answer
Shawn Reichner 33Shawn Reichner 33
Thank you! I had to add more to the user being created, but after that works like a charm!!