I have an email service that creates a new "training" record. the email body is broken down to populate several fields.
Class:
global class ProcessTraining implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
String[] emailBody = email.plainTextBody.split('\n', 0);
Training__c em;
String CertificationAttempts = emailBody[22].substringAfter(': ').trim();
String ScorePercent = emailBody[17].substringAfter(': ').trim();
String TimeSpent = emailBody[11].substringAfter(': ').trim();
String datecomp = emailBody[9].substringAfter(': ');
String TrainingName= emailBody[8].substringAfter(': ').trim();
String ContactEmail = emailBody[7].substringAfter(': ').trim();
String mm=datecomp.substringBefore('/');
String dd=datecomp.substringBetween('/','/');
String yy=datecomp.subStringAfterlast('/').trim();
date myDate=Date.newInstance(integer.valueOf(yy),
integer.valueOf(mm),
integer.valueOf(dd));
em= new Training__c (Message__c = email.plainTextBody);
em.Date_training_completed__c = myDate;
em.Number_of_certification_attempts__c = integer.valueof(CertificationAttempts);
em.Total_time_in_training_course__c = decimal.valueof(TimeSpent);
em.Name = TrainingName;
em.Training_certification_score__c = decimal.valueof(ScorePercent);
Contact con = [SELECT id,email FROM Contact WHERE email = :ContactEmail];
em.Contact__c = con.id;
insert em;
return result;
} //close class
}
Test Coverage:
@isTest
public class ProcessTrainingTest
{
static testMethod void TestinBoundEmail()
{
// create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
// setup the data for the email
email.subject = 'Create Contact';
email.fromAddress = '[email protected]';
email.plainTextBody = 'email body\n2225256325\nTitle:\ntest:';
// call the email service class and test it with the data in the testMethod
ProcessTraining testInbound=new ProcessTraining ();
testInbound.handleInboundEmail(email, env);
}
}
Whenever I run the test I get an error message:
Error Message System.ListException: List index out of bounds: 22 Stack Trace Class.ProcessTraining.handleInboundEmail: line 16, column 1 Class.ProcessTrainingTest.TestinBoundEmail: line 20, column 1
Can you help me out in getting this class covered?