0

I have the following method in my apex class:

@testVisible
private static Custon_Object_Case__c getExistingRecord(Messaging.InboundEmail email){
    List<String> referenceId = email.references;
    
    List<EmailMessage> prevmail = [SELECT RelatedToId from EmailMessage WHERE ThreadIdentifier =: referenceId];
    
    return [SELECT Id FROM Custon_Object_Case__c WHERE Id=: prevmail[0].RelatedToId];
}

This method simply returns the Id of the existing custom object record to which the incoming email belongs to. I wrote the following test method for but it is giving me error:

@IsTest
    static void getExistingRecordTest(){
       Custon_Object_Case__c testTTCase = new Custon_Object_Case__c();
        insert testTTCase;
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
         List<String> referenceId = email.references;
    
    EmailMessage email = new EmailMessage();
    email.FromAddress = 'test@abc.org';
    email.Incoming = True;
    email.ToAddress= 'test@test.com';
    email.ThreadIdentifier = referenceId[0];
    email.Subject = 'Test email';
    email.HtmlBody = 'Test email body';
    email.RelatedToId = testTTCase.Id;
    insert email;

        Test.startTest();
        Custom_Object_Case__c result = TT_CaseEmailService.getExistingRecord(email);
        Test.stopTest();
        EmailMessage relatedId = [SELECT RelatedToId FROM EmailMessage WHERE ThreadIdentifier =: referenceId]; 
        System.assertEquals(result.Id, testTTCase.Id);
        
    }
     

I get the following error:

enter image description here

2
  • so what is line 52 in the test class?
    – cropredy
    Feb 28 at 19:43
  • email.ThreadIdentifier = referenceId[0];
    – Marjan
    Feb 28 at 20:36

2 Answers 2

0

@marjan : Above error is seen since you are not inserting EmailMessage records in test class.

Please try to insert EmailMessage record before Test.startTest() using below code :

EmailMessage email = new EmailMessage();
email.FromAddress = 'test@abc.org';
email.Incoming = True;
email.ToAddress= 'test@test.com';
email.ThreadIdentifier = referenceId[0];
email.Subject = 'Test email';
email.HtmlBody = 'Test email body';
email. RelatedToId = testTTCase.Id; // Link to your custom object record
insert email;

Try to follow best practises for Apex tests - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

Also , below line for assert is wrong in your test class :

System.assertEquals(relatedId.Id, testTTCase.Id);

You can assert the id returned by your method and the custom object inserted in the test method as below :

System.assertEquals(result.Id, testTTCase.Id);
3
  • I updated the question/code based on your suggestions but I still get the same error and test is failing.
    – Marjan
    Feb 28 at 18:49
  • Ok, @cropredy 's answer works
    – Rohit
    Mar 1 at 1:15
  • @Marjan : Do consider upvoting the answers if it helped you.
    – Rohit
    Mar 1 at 4:13
0

Your issue is here in the testmethod:

Messaging.InboundEmail email = new Messaging.InboundEmail() ;
List<String> referenceId = email.references;

the value of email.references is null so the value of the collection referenceId is also null

Thus, the line 52

email.ThreadIdentifier = referenceId[0];

fails because referenceId is null so there's no [0]th element

Since it looks like you are mocking an EmailMessage, line 52 should be something like

email.ThreadIdentifier = 'someValue';

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .