-1

I have written a working class in Apex. It is an Email service extender, that processes incoming emails. It is working perfect on my sandbox enviroment.

I have created a test class, so I can also deploy it to my production, but when validating the code, I get the only 72% of my code is tested.

This is my main class

global class inboundEmail implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

Lead lead;

String [] mFromUserParams;
String [] sourceText;
String mCaseObject; 

try{
    sourceText = email.toAddresses[0].split('@');
    String [] mParams = sourceText[0].split('\\.');
**// FROM THIS LINE TO THE END - NOT COVERED**
    mFromUserParams = email.fromAddress.split('@');
    mCaseObject = mParams[0];
    if (mCaseObject == 'lead'){
        lead = new Lead();
        lead.LastName = mFromUserParams[0];
        lead.Company = email.fromAddress;
        lead.OwnerId = mParams[1];
        lead.LeadSource = mParams[2];
        lead.Email = email.fromAddress;
        lead.RequirementsDescription__c = email.subject + email.plainTextBody;

        insert lead;
        result.success = true;
    }  else if (mCaseObject == 'case'){
        result.success = true;
    }  else {
        result.success = false;
    }
}catch(Exception e){
    result.success = false;
    result.message = 'Oops, I failed.';
}
return result;
}
}

This is my test class

@isTest
private class inboundEmailTest {
public static testMethod void inboundEmail(){     
    // Create a new email, envelope object and Header
    Messaging.InboundEmail email = new Messaging.InboundEmail();
    Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
envelope.toAddress = 'lead.owner.new@cpeneac.cl.apex.sandbox.salesforce.com';
    envelope.fromAddress = 'user@acme.com';
    email.subject = 'Please contact me';
    email.fromName = 'Test From Name';
    email.plainTextBody = 'Hello, this a test email body. for testing  Bye';
    // setup controller object
    inboundEmail catcher = new inboundEmail();
    Messaging.InboundEmailResult result = catcher.handleInboundEmail(email, envelope);
}
}

According to the error message, ALL lines in the Try/Catch block from the 3rd line are not covered. (marked in the code).

Saariko
  • 1,703
  • 4
  • 26
  • 46

3 Answers3

1

in your test method you're setting envelope.toAddress but in your email service you're splitting the first element of the actual InboundEmail objects toAddresses. this probably causes either an ArrayIndexOutOfBoundsException or a NPE because the element 0 does not exist. so the code coverage will be poor because your test always jumps into the exception handling and leaves the rest of you code uncovered. just set the emails toAddresses and you should have a better coverage. h9nry

h9nry
  • 221
  • 1
  • 6
  • yea, I thought that too, but envelope does not have toAddresses[] object. only as a single string object. – Saariko Jul 05 '12 at 16:41
  • 1
    you don't need the envelope object, just set `email.toAddresses = new List{'lead.owner.new@cpeneac.cl.apex.sandbox.salesforce.com'};` – h9nry Jul 05 '12 at 18:06
  • I found that I used inboundEmail as the class name, but InboundEmail is an internal object. Maybe it interferrred or not. also removing the LIST from the email.fromAddress solved this. thanks – Saariko Jul 08 '12 at 07:51
0

In your test code, can you add a scenario that causes the lead insert to fail? This will cause the code in your catch block to execute and provide you the needed code test coverage.

Rajesh
  • 239
  • 2
  • 3
0

The email.fromAddress is not a list by default, so just setting that to a string and not a list solved this.

Saariko
  • 1,703
  • 4
  • 26
  • 46