function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc_1sfdc_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');
        
    }
}
Best Answer chosen by sfdc_1
Maharajan CMaharajan C
Hi,

Add the below hightighted changes then it will work:
 
@isTest
private class  testCreateContactFrmEmail {
    static testMethod void validatedata()
    {
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
        List<Account> accList = [Select Id from Account limit 1];
        email.subject = 'Customer creation in SAP :' + accList[0].id;
        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;
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi,

Add the below hightighted changes then it will work:
 
@isTest
private class  testCreateContactFrmEmail {
    static testMethod void validatedata()
    {
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
        List<Account> accList = [Select Id from Account limit 1];
        email.subject = 'Customer creation in SAP :' + accList[0].id;
        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;
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
sfdc_1sfdc_1
thanks its working with 100% coverage