You need to sign in to do that
Don't have an account?
sfdc_1
Test class code coverage for email service
Hi Can anyone help me out to cover the code coverage of below email serivce class. I am getting 60% coverage with my code. please help me out since i am very new to test class.
class:
global class UpdateCustomerEmail implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope ) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
//String subToCompare = 'Customer creation in SAP : 001C1000002LOrzIFG';
String subToCompare = email.subject;
String accid1=subToCompare.substringAfterLast(':');
String accid= accid1.trim();
system.debug('1st'+accid);
system.debug('2st'+subToCompare);
if(email.subject.equalsIgnoreCase(subToCompare))
{
String getLabelName = Label.Status;
List<Account> aa=new list<account>();
account a= [SELECT Id, Name,Status__c,Customer_Code__c FROM Account WHERE Id =:accid];
system.debug(accid);
a.Customer_Code__c='990';
a.Status__c='Customer created in SAP';
aa.add(a);
update aa;
} return result;
}
}
My test class:
@isTest
private class testCreateContactFrmEmail {
static testMethod void validatedata()
{
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
email.subject = 'Customer creation in SAP :';
email.plainTextBody = 'FromEmail';
//env.fromAddress = 'abcd@test.com';
UpdateCustomerFromEmail upc=new UpdateCustomerFromEmail();
upc.handleInboundEmail(email, env );
Messaging.InboundEmailResult result = upc.handleInboundEmail(email, env);
System.assertEquals( result.success ,true);
}
@testSetup static void setup() {
// Create common test accounts
List<Account> testAccts = new List<Account>();
for(Integer i=0;i<100;i++) {
testAccts.add(new Account(Name = 'TestAcct'+i,Status__c='Customer created in SAP',Customer_Code__c='990'));
}
insert testAccts;
}
static testMethod void validate3(){
list<account> testAccts=new list<account>();
Account testAcs = [SELECT Id,Status__c,Customer_Code__c FROM Account WHERE Name='TestAcct0' LIMIT 1];
// Modify first account
testAcs.Customer_Code__c = '990';
// This update is local to this test method only.
testAccts.add(testAcs);
Test.startTest();
update testAccts;
Test.stopTest();
// System.assertEquals('990',testAcs.Customer_Code__c);
//System.assertEquals('990', '990');
}
}
class:
global class UpdateCustomerEmail implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope ) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
//String subToCompare = 'Customer creation in SAP : 001C1000002LOrzIFG';
String subToCompare = email.subject;
String accid1=subToCompare.substringAfterLast(':');
String accid= accid1.trim();
system.debug('1st'+accid);
system.debug('2st'+subToCompare);
if(email.subject.equalsIgnoreCase(subToCompare))
{
String getLabelName = Label.Status;
List<Account> aa=new list<account>();
account a= [SELECT Id, Name,Status__c,Customer_Code__c FROM Account WHERE Id =:accid];
system.debug(accid);
a.Customer_Code__c='990';
a.Status__c='Customer created in SAP';
aa.add(a);
update aa;
} return result;
}
}
My test class:
@isTest
private class testCreateContactFrmEmail {
static testMethod void validatedata()
{
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
email.subject = 'Customer creation in SAP :';
email.plainTextBody = 'FromEmail';
//env.fromAddress = 'abcd@test.com';
UpdateCustomerFromEmail upc=new UpdateCustomerFromEmail();
upc.handleInboundEmail(email, env );
Messaging.InboundEmailResult result = upc.handleInboundEmail(email, env);
System.assertEquals( result.success ,true);
}
@testSetup static void setup() {
// Create common test accounts
List<Account> testAccts = new List<Account>();
for(Integer i=0;i<100;i++) {
testAccts.add(new Account(Name = 'TestAcct'+i,Status__c='Customer created in SAP',Customer_Code__c='990'));
}
insert testAccts;
}
static testMethod void validate3(){
list<account> testAccts=new list<account>();
Account testAcs = [SELECT Id,Status__c,Customer_Code__c FROM Account WHERE Name='TestAcct0' LIMIT 1];
// Modify first account
testAcs.Customer_Code__c = '990';
// This update is local to this test method only.
testAccts.add(testAcs);
Test.startTest();
update testAccts;
Test.stopTest();
// System.assertEquals('990',testAcs.Customer_Code__c);
//System.assertEquals('990', '990');
}
}
Add the below hightighted changes then it will work:
Thanks,
Maharajan.C
All Answers
Add the below hightighted changes then it will work:
Thanks,
Maharajan.C