0

I'm trying to build a test class for me to move an Email Service class into Prod, but I'm getting this error.

Here is part of my code (The important bits)

Email Service Class

global class emailInbdounds implements Messaging.InboundEmailHandler{

global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env)
{
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    String myPlainText= '';
    myPlainText =email.subject + '\n' +  email.plainTextBody;

Test Class

@isTest(seeAlldata=True) private class testSendEmail {

@isTest static void testIdontKnow(){
    Messaging.InboundEmail email = new Messaging.InboundEmail() ;
    Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();

    email.subject = 'Test';
    email.fromName = 'test test';
    email.plainTextBody = 'Hello, this a test';

Messaging.InboundEmail.BinaryAttachment[] binaryAttachments = new Messaging.InboundEmail.BinaryAttachment[1];  
Messaging.InboundEmail.BinaryAttachment binaryAttachment = new Messaging.InboundEmail.BinaryAttachment();
binaryAttachment.Filename = 'test.txt';
String algorithmName = 'HMacSHA1';

Blob b = Crypto.generateMac(algorithmName, Blob.valueOf('test'),Blob.valueOf('test_key'));
binaryAttachment.Body = b;
binaryAttachments[0] =  binaryAttachment ;
email.binaryAttachments = binaryAttachments ;
envelope.fromAddress = '[email protected]';

emailInbdounds catcher = new emailInbdounds();

catcher.handleInboundEmail(email, envelope);


           
}



}

The error comes on the last line :

catcher.handleInboundEmail(email, envelope);

Based on my research , this error appears when the Data Type you are creating has not been initialized, but, the line prior to this one, already does that.

emailInbdounds catcher = new emailInbdounds();
2
  • 1
    Seems like the null reference exception is occurring inside the handleInboundEmail method. So, unless you post the code for this method, I doubt anyone would be able help you out.
    – arut
    Commented Oct 30, 2020 at 5:21
  • You are absolutely right, the issue was related to some strings that when testing the method, were not getting initialized. It worked just as a Class, but when testing , it failed.
    – Neto0291
    Commented Oct 30, 2020 at 6:33

1 Answer 1

1

After searching a ton of information and just trying to fix the testMethod, I started debugging the actual Email service Class, and that's where I found the error. For some reason, the Email Service class is working fine on its own, but when testing it, the List of Strings I had setup, were not getting initialized, hence the error.

Hope this helps someone in the future. TLDR: You need to make sure your Email Service class works fine when testing, and not only on its own!!

You must log in to answer this question.

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