You need to sign in to do that
Don't have an account?
SalesforceUser11
code coverage issue of test class
Hello,
I am wrirting test class. I have included all the objects in my test class but its coverage is 0%. Can anyone suggest me what to do so test coverage will increase.
Apex Class:
/**
* Email services are automated processes that use Apex classes
* to process the contents, headers, and attachments of inbound
* email.
Create book record from the inbound email
*/
global class InboundEmailtoVacancy implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Review_Vacancy__c vacancy = new Review_Vacancy__c();
//----Insert Record
vacancy.Email_Subject__c = email.subject;
vacancy.From_Address__c = email.fromAddress;
if(email.htmlBody == Null){
vacancy.Email_Body__c = email.plainTextBody;
}
else
vacancy.Email_Body__c = email.htmlBody;
String emailBody1 = email.plainTextBody;
String s1 = 'Order Reference:';
String s2 = 'Job:';
String s3 = 'Job Location:';
String s4 = 'StartDate:';
String s5 = 'EndDate:';
String s6 = 'Start Time';
String s7 = 'End Time';
String s8 = 'Number Required:';
String s9 = 'Additional Details';
String s10 = 'Borough:';
//-----Order Num
String AfterS1 = emailBody1.substringAfter(s1);
String OrRefS1 = AfterS1.substringBefore(s2);
System.debug('AfterS1--Order Ref----'+AfterS1);
System.debug('OrRefS1--Order Ref----'+OrRefS1);
//-----Job
String AfterS2 = emailBody1.substringAfter(s2);
String OrRefS2 = AfterS2.substringBefore(s3);
//-----Job Location
String AfterS3 = emailBody1.substringAfter(s3);
String OrRefS3 = AfterS3.substringBefore(s4);
//-----StartDate
String AfterS4 = emailBody1.substringAfter(s4);
String OrRefS4 = AfterS4.substringBefore(s5);
//-----EndDate
String AfterS5 = emailBody1.substringAfter(s5);
String OrRefS5 = AfterS5.substringBefore(s6);
//-----Start Time
String AfterS6 = emailBody1.substringAfter(s6);
String OrRefS6 = AfterS6.substringBefore(s7);
//-----End Time
String AfterS7 = emailBody1.substringAfter(s7);
String OrRefS7 = AfterS7.substringBefore(s8);
//-----Number Required
String AfterS8 = emailBody1.substringAfter(s8);
String OrRefS8 = AfterS8.substringBefore(s9);
//-----Borough
String AfterS10 = emailBody1.substringAfter(s10);
String OrRefS10 = AfterS10.substringBefore(s9);
//-----Additional Details
String AfterS9 = emailBody1.substringAfter(s9);
//String OrRefS9 = AfterS9.substringBefore(s10);
vacancy.Order_Reference__c = OrRefS1;
vacancy.Job__c = OrRefS2;
vacancy.Job_Location__c = OrRefS3;
vacancy.StartDate__c = OrRefS4;
vacancy.EndDate__c = OrRefS5;
vacancy.Start_Time__c = OrRefS6;
vacancy.End_Time__c = OrRefS7;
vacancy.Number_Required__c = OrRefS8;
vacancy.Borough__c = OrRefS10;
//vacancy.Additional_Details__c = OrRefS9;
insert vacancy ;
// Save attachments, if any Image or text file
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 = vacancy.Id;
insert attachment;
}
}
// Save Email as PDF attachments
if(email.htmlBody == Null){
Attachment attachment1 = new Attachment();
attachment1.Name = 'EmailBody';
attachment1.Body = Blob.valueOf(email.plainTextBody);
attachment1.ParentId = vacancy.Id;
insert attachment1;
}
else
{
Attachment attachment1 = new Attachment();
attachment1.Name = 'EmailBody';
attachment1.Body = Blob.valueOf(email.htmlBody);
attachment1.ParentId = vacancy.Id;
insert attachment1;
}
result.success = true;
return result;
}
}
Test Class:
//Test Method for main class
@isTest
private class InboundEmailtoVacancyTestTest{
static testMethod void testEmailServiceHtmlBodyNull()
{
// 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 = 'Test Job Applicant';
email.fromAddress = 'someaddress@email.com';
email.htmlBody = null;
email.plainTextBody = 'Test plainTextBody';
env.fromAddress = 'someaddress@email.com';
Review_Vacancy__c vacancy = new Review_Vacancy__c();
system.debug(' email.htmlBody1 ************************'+email.htmlBody);
vacancy.Email_Body__c = email.plainTextBody;
vacancy.Email_Subject__c = email.subject;
vacancy.From_Address__c = email.fromAddress;
insert vacancy;
system.debug('Insert Vacancy'+vacancy);
// add an Binary attachment
// 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 };
// add an Text atatchment
Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
Attachment attachment = new Attachment();
attachment.Name = 'textfileone.txt';
attachment.body = blob.valueOf('my attachment text');
attachment.ParentId = vacancy.Id;
insert attachment;
email.textAttachments = new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
// call the email service class and test it with the data in the testMethod
/// inBoundEmail testInbound=new inBoundEmail();
//testInbound.handleInboundEmail(email, env);
// Create Attachmenat data
Attachment attachmnt =new Attachment();
attachmnt.name='textfileone.txt';
attachmnt.body =blob.valueOf('my attachment text');
attachmnt.ParentId =vacancy.Id;
insert attachmnt ;
}
static testMethod void testEmailServiceHtmlBodyNotNull() {
system.debug('testEmailService testEmailServiceHtmlBodyNotNull***************');
// create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
system.debug('Inbound Email testEmailServiceHtmlBodyNotNull**********************');
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
system.debug('Inbound Envelope testEmailServiceHtmlBodyNotNull**********************');
email.subject = 'Test Job Applicant';
email.fromname = 'FirstName LastName';
email.htmlBody = 'Test htmlBody';
email.plainTextBody = 'Test plainTextBody';
env.fromAddress = 'someaddress@email.com';
Review_Vacancy__c vacancy = new Review_Vacancy__c();
vacancy.Email_Subject__c = email.subject;
vacancy.From_Address__c = email.fromAddress;
system.debug(' Review_Vacancy__c Obj testEmailServiceHtmlBodyNotNull************************');
system.debug(' email.htmlBody testEmailServiceHtmlBodyNotNull ************************'+email.htmlBody);
String s1;
String s2;
String s3;
String s4;
vacancy.Email_Body__c = email.htmlBody;
String emailBody1 = email.plainTextBody;
s1 = 'Order Reference:';
s2 = 'Job:';
s3 = 'Job Location:';
s4 = 'StartDate:';
String s5 = 'EndDate:';
String s6 = 'Start Time';
String s7 = 'End Time';
String s8 = 'Number Required:';
String s9 = 'Additional Details';
String s10 = 'Borough:';
//-----Order Num
String AfterS1 = s1;
String OrRefS1 = s2;
String AfterS2 = s2;
String OrRefS2 = s3;
//-----Job Location
String AfterS3 = s3;
String OrRefS3 = s4;
//-----StartDate
String AfterS4 = s4;
String OrRefS4 = s5;
//-----EndDate
String AfterS5 = s5;
String OrRefS5 = s6;
//-----Start Time
String AfterS6 = s6;
String OrRefS6 = s7;
//-----End Time
String AfterS7 = s7;
String OrRefS7 = s8;
//-----Number Required
String AfterS8 = s8;
String OrRefS8 = s9;
//-----Borough
String AfterS10 = s10;
String OrRefS10 = s9;
//-----Additional Details
String AfterS9 = s9;
vacancy.Order_Reference__c = OrRefS1;
vacancy.Job__c = OrRefS2;
vacancy.Job_Location__c = OrRefS3;
vacancy.StartDate__c = OrRefS4;
vacancy.EndDate__c = OrRefS5;
vacancy.Start_Time__c = OrRefS6;
vacancy.End_Time__c = OrRefS7;
vacancy.Number_Required__c = OrRefS8;
vacancy.Borough__c = OrRefS10;
insert vacancy;
system.debug('Insert Vacancy testEmailServiceHtmlBodyNotNull'+vacancy);
}
}
Thanks,
Utz
I am wrirting test class. I have included all the objects in my test class but its coverage is 0%. Can anyone suggest me what to do so test coverage will increase.
Apex Class:
/**
* Email services are automated processes that use Apex classes
* to process the contents, headers, and attachments of inbound
* email.
Create book record from the inbound email
*/
global class InboundEmailtoVacancy implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
Review_Vacancy__c vacancy = new Review_Vacancy__c();
//----Insert Record
vacancy.Email_Subject__c = email.subject;
vacancy.From_Address__c = email.fromAddress;
if(email.htmlBody == Null){
vacancy.Email_Body__c = email.plainTextBody;
}
else
vacancy.Email_Body__c = email.htmlBody;
String emailBody1 = email.plainTextBody;
String s1 = 'Order Reference:';
String s2 = 'Job:';
String s3 = 'Job Location:';
String s4 = 'StartDate:';
String s5 = 'EndDate:';
String s6 = 'Start Time';
String s7 = 'End Time';
String s8 = 'Number Required:';
String s9 = 'Additional Details';
String s10 = 'Borough:';
//-----Order Num
String AfterS1 = emailBody1.substringAfter(s1);
String OrRefS1 = AfterS1.substringBefore(s2);
System.debug('AfterS1--Order Ref----'+AfterS1);
System.debug('OrRefS1--Order Ref----'+OrRefS1);
//-----Job
String AfterS2 = emailBody1.substringAfter(s2);
String OrRefS2 = AfterS2.substringBefore(s3);
//-----Job Location
String AfterS3 = emailBody1.substringAfter(s3);
String OrRefS3 = AfterS3.substringBefore(s4);
//-----StartDate
String AfterS4 = emailBody1.substringAfter(s4);
String OrRefS4 = AfterS4.substringBefore(s5);
//-----EndDate
String AfterS5 = emailBody1.substringAfter(s5);
String OrRefS5 = AfterS5.substringBefore(s6);
//-----Start Time
String AfterS6 = emailBody1.substringAfter(s6);
String OrRefS6 = AfterS6.substringBefore(s7);
//-----End Time
String AfterS7 = emailBody1.substringAfter(s7);
String OrRefS7 = AfterS7.substringBefore(s8);
//-----Number Required
String AfterS8 = emailBody1.substringAfter(s8);
String OrRefS8 = AfterS8.substringBefore(s9);
//-----Borough
String AfterS10 = emailBody1.substringAfter(s10);
String OrRefS10 = AfterS10.substringBefore(s9);
//-----Additional Details
String AfterS9 = emailBody1.substringAfter(s9);
//String OrRefS9 = AfterS9.substringBefore(s10);
vacancy.Order_Reference__c = OrRefS1;
vacancy.Job__c = OrRefS2;
vacancy.Job_Location__c = OrRefS3;
vacancy.StartDate__c = OrRefS4;
vacancy.EndDate__c = OrRefS5;
vacancy.Start_Time__c = OrRefS6;
vacancy.End_Time__c = OrRefS7;
vacancy.Number_Required__c = OrRefS8;
vacancy.Borough__c = OrRefS10;
//vacancy.Additional_Details__c = OrRefS9;
insert vacancy ;
// Save attachments, if any Image or text file
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 = vacancy.Id;
insert attachment;
}
}
// Save Email as PDF attachments
if(email.htmlBody == Null){
Attachment attachment1 = new Attachment();
attachment1.Name = 'EmailBody';
attachment1.Body = Blob.valueOf(email.plainTextBody);
attachment1.ParentId = vacancy.Id;
insert attachment1;
}
else
{
Attachment attachment1 = new Attachment();
attachment1.Name = 'EmailBody';
attachment1.Body = Blob.valueOf(email.htmlBody);
attachment1.ParentId = vacancy.Id;
insert attachment1;
}
result.success = true;
return result;
}
}
Test Class:
//Test Method for main class
@isTest
private class InboundEmailtoVacancyTestTest{
static testMethod void testEmailServiceHtmlBodyNull()
{
// 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 = 'Test Job Applicant';
email.fromAddress = 'someaddress@email.com';
email.htmlBody = null;
email.plainTextBody = 'Test plainTextBody';
env.fromAddress = 'someaddress@email.com';
Review_Vacancy__c vacancy = new Review_Vacancy__c();
system.debug(' email.htmlBody1 ************************'+email.htmlBody);
vacancy.Email_Body__c = email.plainTextBody;
vacancy.Email_Subject__c = email.subject;
vacancy.From_Address__c = email.fromAddress;
insert vacancy;
system.debug('Insert Vacancy'+vacancy);
// add an Binary attachment
// 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 };
// add an Text atatchment
Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
Attachment attachment = new Attachment();
attachment.Name = 'textfileone.txt';
attachment.body = blob.valueOf('my attachment text');
attachment.ParentId = vacancy.Id;
insert attachment;
email.textAttachments = new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
// call the email service class and test it with the data in the testMethod
/// inBoundEmail testInbound=new inBoundEmail();
//testInbound.handleInboundEmail(email, env);
// Create Attachmenat data
Attachment attachmnt =new Attachment();
attachmnt.name='textfileone.txt';
attachmnt.body =blob.valueOf('my attachment text');
attachmnt.ParentId =vacancy.Id;
insert attachmnt ;
}
static testMethod void testEmailServiceHtmlBodyNotNull() {
system.debug('testEmailService testEmailServiceHtmlBodyNotNull***************');
// create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
system.debug('Inbound Email testEmailServiceHtmlBodyNotNull**********************');
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
system.debug('Inbound Envelope testEmailServiceHtmlBodyNotNull**********************');
email.subject = 'Test Job Applicant';
email.fromname = 'FirstName LastName';
email.htmlBody = 'Test htmlBody';
email.plainTextBody = 'Test plainTextBody';
env.fromAddress = 'someaddress@email.com';
Review_Vacancy__c vacancy = new Review_Vacancy__c();
vacancy.Email_Subject__c = email.subject;
vacancy.From_Address__c = email.fromAddress;
system.debug(' Review_Vacancy__c Obj testEmailServiceHtmlBodyNotNull************************');
system.debug(' email.htmlBody testEmailServiceHtmlBodyNotNull ************************'+email.htmlBody);
String s1;
String s2;
String s3;
String s4;
vacancy.Email_Body__c = email.htmlBody;
String emailBody1 = email.plainTextBody;
s1 = 'Order Reference:';
s2 = 'Job:';
s3 = 'Job Location:';
s4 = 'StartDate:';
String s5 = 'EndDate:';
String s6 = 'Start Time';
String s7 = 'End Time';
String s8 = 'Number Required:';
String s9 = 'Additional Details';
String s10 = 'Borough:';
//-----Order Num
String AfterS1 = s1;
String OrRefS1 = s2;
String AfterS2 = s2;
String OrRefS2 = s3;
//-----Job Location
String AfterS3 = s3;
String OrRefS3 = s4;
//-----StartDate
String AfterS4 = s4;
String OrRefS4 = s5;
//-----EndDate
String AfterS5 = s5;
String OrRefS5 = s6;
//-----Start Time
String AfterS6 = s6;
String OrRefS6 = s7;
//-----End Time
String AfterS7 = s7;
String OrRefS7 = s8;
//-----Number Required
String AfterS8 = s8;
String OrRefS8 = s9;
//-----Borough
String AfterS10 = s10;
String OrRefS10 = s9;
//-----Additional Details
String AfterS9 = s9;
vacancy.Order_Reference__c = OrRefS1;
vacancy.Job__c = OrRefS2;
vacancy.Job_Location__c = OrRefS3;
vacancy.StartDate__c = OrRefS4;
vacancy.EndDate__c = OrRefS5;
vacancy.Start_Time__c = OrRefS6;
vacancy.End_Time__c = OrRefS7;
vacancy.Number_Required__c = OrRefS8;
vacancy.Borough__c = OrRefS10;
insert vacancy;
system.debug('Insert Vacancy testEmailServiceHtmlBodyNotNull'+vacancy);
}
}
Thanks,
Utz
Please try the below Test class with 82% Coverage, In your test class you have missed some code:
Please make it Best Answer if it helps you.
Thanks
Ajay Dubedi
You didn't call the class object in test class like Please let me know if this help!
Thanks
Shivdeep