You need to sign in to do that
Don't have an account?
Jonas Myhre
Need some help for a test class
Hello! I've written an email service which i need to test the code coverage for, and increase it. Its currently at 62%, and needs to be minimun 75%. Im stuck, can anyone help me, or point me in the right direction? Here is the code, i appreciate help extremely much! :
global class EmailToLead implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Lead lead = new Lead();
lead.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
lead.LastName = email.fromname.substring(email.fromname.indexOf(' '));
lead.Email = envelope.fromAddress;
lead.Title = email.subject;
lead.LeadSource = 'Market Intel';
String textBody = email.plainTextBody;
lead.Description = textBody;
String companyName;
if(textBody.indexOf('CompanyName:') > -1) {
Integer startPos = textBody.indexOf('CompanyName:');
Integer endPos = textBody.indexOf('.',textBody.indexOf('CompanyName:'));
companyName = textBody.substring(startPos+'CompanyName:'.length(),endPos);
}
lead.Company = companyName != NULL ? companyName : '(not provided)';
lead.Status = 'Web';
insert lead;
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
Attachment attachment = new Attachment();
// attach to the newly created contact record
attachment.ParentId = lead.Id;
attachment.Name = email.binaryAttachments[i].filename;
attachment.Body = email.binaryAttachments[i].body;
insert attachment;
}
}
return result;
}
}
global class EmailToLead implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Lead lead = new Lead();
lead.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
lead.LastName = email.fromname.substring(email.fromname.indexOf(' '));
lead.Email = envelope.fromAddress;
lead.Title = email.subject;
lead.LeadSource = 'Market Intel';
String textBody = email.plainTextBody;
lead.Description = textBody;
String companyName;
if(textBody.indexOf('CompanyName:') > -1) {
Integer startPos = textBody.indexOf('CompanyName:');
Integer endPos = textBody.indexOf('.',textBody.indexOf('CompanyName:'));
companyName = textBody.substring(startPos+'CompanyName:'.length(),endPos);
}
lead.Company = companyName != NULL ? companyName : '(not provided)';
lead.Status = 'Web';
insert lead;
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
Attachment attachment = new Attachment();
// attach to the newly created contact record
attachment.ParentId = lead.Id;
attachment.Name = email.binaryAttachments[i].filename;
attachment.Body = email.binaryAttachments[i].body;
insert attachment;
}
}
return result;
}
}
Each test should follow the following structure:
In your case, the setup will consist of building a new InboundEmail object and then passing that into your method
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.
[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
All Answers
NOTE: Please use the "Add a code sample" button (icon of <>) to increase readability
Each test should follow the following structure:
In your case, the setup will consist of building a new InboundEmail object and then passing that into your method
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.
[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/