You need to sign in to do that
Don't have an account?
Code Coverage Issue
I am only able to get 26% code coverage but not sure why
@isTest
public Class TestEamilAttchment{
Static testMethod Void TestinBoundEmail()
{
//create a new email and envelope ojbect
messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
//setup the data for the email
email.subject='Create Service Call Log';
env.fromAddress='Tmonson@cms4.com' (mailto:env.fromAddress='Tmonson@cms4.com');
email.plainTextBody='Email body\n2332345354454\nTitle';
email.FromName ='Thomas Monson';
Service_Call_Log__C sService_Call_Log = new Service_Call_Log__C();
sService_call_log.Contact__C='00330000012mk3O';
//add an Binary attachment
Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
attachment.body = blob.valueOf('My attachmeent text');
attachment.fileName = 'textfileone.txt';
attachment.mimeTypeSubType='Text/Plain';
email.binaryAttachments=new Messaging.inboundEmail.BinaryAttachment[]{attachment};
//add an Text attachmet
Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
attachmenttext.body = 'my attachment text';
attachmenttext.fileName = 'textfiletwo3.txt';
attachmenttext.mimeTypeSubType='texttwo/plain';
email.textAttachments = new Messaging.inboundEmail.TextAttachment[]{attachmenttext};
//call the email serivce class and text it with the data in the textMethod
EmailToServiceCallLog testInbound= new EmailToServiceCallLog ();
testInbound.handleInboundEmail(email,env);
}
}
global class EmailToServiceCallLog implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Service_Call_Log__C sService_Call_Log = new Service_Call_Log__C();
try
{
Contact vCon=[SELECT ID, Name, Phone, Email FROM Contact WHERE email = :email.fromAddress LIMIT 1];
sService_Call_Log.Contact__C= vCon.id;
sService_Call_Log.Contact_Phone_Number__C=vCon.Phone;
sService_Call_Log.Job__C = 'a013000000UHggN';
sService_Call_Log.Subject__C = email.subject;
sService_Call_Log.description__C= email.plainTextBody;
sService_Call_Log.Web_Email__C=email.FromAddress;
sService_Call_Log.Web_name__C=email.FromName;
insert sService_Call_log;
if (email.textAttachments != null)
{
for(Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments)
{
Attachment attachment = new Attachment();
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = sService_Call_log.Id;
insert attachment;
}
}
//Save any Binary Attachment
if (email.binaryAttachments != null)
{
for(Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
Attachment attachment = new Attachment();
attachment.Name = bAttachment.fileName;
attachment.Body = bAttachment.body;
attachment.ParentId = sService_Call_log.ID;
insert attachment;
}
}
}
Catch(QueryException e){
system.debug('Query Issue: '+e);
}
result.success = true;
return result;
}
}
Please refer to links below that will help you to increase the code coverage:
1.http://salesforce.stackexchange.com/questions/25838/test-class-for-sending-email-with-attachment
2.https://developer.salesforce.com/forums/ForumsMain?id=906F00000009EZNIA2
3.https://developer.salesforce.com/page/Code_Sample_-_Testing_Email_Services_with_Inbound_Attachments
4.http://sfdcfaq.blogspot.com/2013/04/test-class-for-email-service-using-apex.html
5.http://knthornt.wordpress.com/category/test-method/
6.http://srinivas4sfdc.blogspot.com/2013/11/test-class-example-for.html
I hope this helps. Please mark this as a "Best Answer" if this has resolved your issue.
//insert contact
contact con = new contact(lastname='testcontact',email='testemail@gmail.com');
inset con,
please post highlited test class coverage lines (green, red), so that will be easy to everyone figure out the problem.