You need to sign in to do that
Don't have an account?
Anu Singh 40
Test class for InboundMessaging
Hi All
I have to write a test class for following class. I wrote test class for my apex class but it only giving 35% coverage.
can anyone help me
Thanks in Advance
global class Handle_Incoming_Email_Forwarding implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope env)
{
String myPlainText= '';
String toAdd=(String.valueOf(email.toAddresses).contains('(') && String.valueOf(email.toAddresses).contains(')')) ? String.valueOf(email.toAddresses).replace('(','').replace(')','') : String.valueOf(email.toAddresses);
String ccAdd=email.ccAddresses == NULL ? '' : (String.valueOf(email.ccAddresses).contains('(') && String.valueOf(email.ccAddresses).contains(')'))? String.valueOf(email.ccAddresses).replace ('(','').replace(')','') : String.valueOf(email.ccAddresses);
myPlainText = 'To: '+toAdd+'\n'+ 'CC: '+ccAdd+ '\n'+ 'From Addresses: '+String.valueOf(email.fromAddress).replace('(','').replace(')','')+ '\n'+ 'Subject: '+email.subject+ '\n'+email.plainTextBody;
Task[] newTask = new Task[0];
Task[] newTask1 = new task[0];
Task[] newTask2 = new task[0];
try {
Id UserId= [SELECT Id, Name, Email FROM User WHERE Email =:email.toAddresses LIMIT 1].Id;
for(Lead obj: [SELECT Id, Name, Email FROM Lead WHERE Email = :email.fromAddress]){
task t=new Task(
ActivityDate = System.today(),
Description = myPlainText,
Priority = 'Normal',
Status = 'Completed',
Subject = 'Email: '+email.subject,
IsReminderSet = true,
TaskSubtype='Email',
RecurrenceStartDateOnly = System.today(),
WhoId = obj.ID);
insert t;
EmailMessage e = new EmailMessage();
e.ActivityId=t.id;
newTask2.add(new Task(
Priority = 'Normal',
Status = 'Inbound Email',
Subject = 'Reply',
IsReminderSet = true,
Description = myPlainText,
ActivityDate = System.today()+7,
OwnerId= UserId,
//ActivityDate = System.today().addDays(7),
RecurrenceStartDateOnly = System.today(),
WhoId = obj.ID
));
}
Id UserId= [SELECT Id, Name, Email FROM User WHERE Email =:email.toAddresses LIMIT 1].Id;
for(Lead obj: [SELECT Id, Name, Email FROM Lead WHERE Email = :email.fromAddress]){
task t=new Task(
ActivityDate = System.today(),
Description = myPlainText,
Priority = 'Normal',
Status = 'Completed',
Subject = 'Email: '+email.subject,
IsReminderSet = true,
TaskSubtype='Email',
RecurrenceStartDateOnly = System.today(),
WhoId = obj.ID);
insert t;
EmailMessage e = new EmailMessage();
e.ActivityId=t.id;
newTask2.add(new Task(
Priority = 'Normal',
Status = 'Inbound Email',
Subject = 'Reply',
IsReminderSet = true,
Description = myPlainText,
ActivityDate = System.today()+7,
OwnerId= UserId,
//ActivityDate = System.today().addDays(7),
RecurrenceStartDateOnly = System.today(),
WhoId = obj.ID
));
}
test class:
@istest
public class TEST_Handle_Incoming_Email_Forwading {
static testMethod void TestinBoundEmail()
{
String contactEmail = 'contactEmail@test.com';
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
email.subject = 'Create Contact';
email.fromAddress = contactEmail;
email.ccAddresses = (new String[] { 'person2@salesforce.com', 'person2@salesforce.com' });
email.fromName='test';
email.htmlBody='this is Html Body for email';
email.plainTextBody = 'email body';
email.toAddresses=(new String[] { 'test@salesforce.com', 'test2@salesforce.com' });
email.replyTo='reply@email.com';
Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
attachment.body = blob.valueOf('my attachment text');
attachment.fileName = 'textfileone.txt';
attachment.mimeTypeSubType = 'text/plain';
email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };
Handle_Incoming_Email_Forwarding testEmail = new Handle_Incoming_Email_Forwarding();
testEmail.handleInboundEmail(email, env);
}
}
for loop is not coverd by this test class. What else I can write to cover this class
I have to write a test class for following class. I wrote test class for my apex class but it only giving 35% coverage.
can anyone help me
Thanks in Advance
global class Handle_Incoming_Email_Forwarding implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope env)
{
String myPlainText= '';
String toAdd=(String.valueOf(email.toAddresses).contains('(') && String.valueOf(email.toAddresses).contains(')')) ? String.valueOf(email.toAddresses).replace('(','').replace(')','') : String.valueOf(email.toAddresses);
String ccAdd=email.ccAddresses == NULL ? '' : (String.valueOf(email.ccAddresses).contains('(') && String.valueOf(email.ccAddresses).contains(')'))? String.valueOf(email.ccAddresses).replace ('(','').replace(')','') : String.valueOf(email.ccAddresses);
myPlainText = 'To: '+toAdd+'\n'+ 'CC: '+ccAdd+ '\n'+ 'From Addresses: '+String.valueOf(email.fromAddress).replace('(','').replace(')','')+ '\n'+ 'Subject: '+email.subject+ '\n'+email.plainTextBody;
Task[] newTask = new Task[0];
Task[] newTask1 = new task[0];
Task[] newTask2 = new task[0];
try {
Id UserId= [SELECT Id, Name, Email FROM User WHERE Email =:email.toAddresses LIMIT 1].Id;
for(Lead obj: [SELECT Id, Name, Email FROM Lead WHERE Email = :email.fromAddress]){
task t=new Task(
ActivityDate = System.today(),
Description = myPlainText,
Priority = 'Normal',
Status = 'Completed',
Subject = 'Email: '+email.subject,
IsReminderSet = true,
TaskSubtype='Email',
RecurrenceStartDateOnly = System.today(),
WhoId = obj.ID);
insert t;
EmailMessage e = new EmailMessage();
e.ActivityId=t.id;
newTask2.add(new Task(
Priority = 'Normal',
Status = 'Inbound Email',
Subject = 'Reply',
IsReminderSet = true,
Description = myPlainText,
ActivityDate = System.today()+7,
OwnerId= UserId,
//ActivityDate = System.today().addDays(7),
RecurrenceStartDateOnly = System.today(),
WhoId = obj.ID
));
}
Id UserId= [SELECT Id, Name, Email FROM User WHERE Email =:email.toAddresses LIMIT 1].Id;
for(Lead obj: [SELECT Id, Name, Email FROM Lead WHERE Email = :email.fromAddress]){
task t=new Task(
ActivityDate = System.today(),
Description = myPlainText,
Priority = 'Normal',
Status = 'Completed',
Subject = 'Email: '+email.subject,
IsReminderSet = true,
TaskSubtype='Email',
RecurrenceStartDateOnly = System.today(),
WhoId = obj.ID);
insert t;
EmailMessage e = new EmailMessage();
e.ActivityId=t.id;
newTask2.add(new Task(
Priority = 'Normal',
Status = 'Inbound Email',
Subject = 'Reply',
IsReminderSet = true,
Description = myPlainText,
ActivityDate = System.today()+7,
OwnerId= UserId,
//ActivityDate = System.today().addDays(7),
RecurrenceStartDateOnly = System.today(),
WhoId = obj.ID
));
}
test class:
@istest
public class TEST_Handle_Incoming_Email_Forwading {
static testMethod void TestinBoundEmail()
{
String contactEmail = 'contactEmail@test.com';
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
email.subject = 'Create Contact';
email.fromAddress = contactEmail;
email.ccAddresses = (new String[] { 'person2@salesforce.com', 'person2@salesforce.com' });
email.fromName='test';
email.htmlBody='this is Html Body for email';
email.plainTextBody = 'email body';
email.toAddresses=(new String[] { 'test@salesforce.com', 'test2@salesforce.com' });
email.replyTo='reply@email.com';
Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
attachment.body = blob.valueOf('my attachment text');
attachment.fileName = 'textfileone.txt';
attachment.mimeTypeSubType = 'text/plain';
email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };
Handle_Incoming_Email_Forwarding testEmail = new Handle_Incoming_Email_Forwarding();
testEmail.handleInboundEmail(email, env);
}
}
for loop is not coverd by this test class. What else I can write to cover this class
You have to insert the lead record in test class to cover the for loop:
Thanks,
Maharajan.C
Thank you for reply. I have already tried test class with lead but I am geeting same 35% coverage. After your reply I tried with user and profile but this time also 35 %
Is there any other mistake I am making or I am have consider to craete a lead data. Lead has some cutom fields which are mandatory. is it may be a case to fail lead insertion.
Thanks,
Anu Singh
Not sure what exactly you want to get from your code. I see too many issues as it is not following the apex best practices.
As per my understanding, It looks like you want to create Two Task Per email for the lead.
You also didn't provide full code and also didn't undertand the purpose of doing the following in the code. First we need to fix your code before we look into the converage. I made a little effort per my understanding and commented all your code instead of removing it. Let me know if this work as you expected.