+ Start a Discussion
Sachin Singh 1Sachin Singh 1 

Test Class for Email Service

Hi, I am new to SDFC and have created a email Service,the email created is workign as expected. 
But i am not able to write the test class for this.
My code for email servie is 
*******************************************************************
global class MyemailService implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
              
          Task[] newTask = new Task[0];
          string IdfromSubject = email.subject.right(9);  
          system.debug('*****Records Unique Identifier is*****'+ IdfromSubject );
          
          if (IdfromSubject.left(2)=='UG'){
              Unclaimed_Gifts__c  Ugifts = new   Unclaimed_Gifts__c ();
              Ugifts= [select id from Unclaimed_Gifts__c where Unique_Identifier__c= : IdfromSubject];
             
              System.debug('Unclaimed Gift Id*******' + Ugifts.Id);
              newTask.add(new Task(Description = email.plainTextBody,
              Priority = 'Normal',           
              Subject = email.subject,
              IsReminderSet = true,           
              WhatId = Ugifts.Id));
          }
          else if(IdfromSubject.left(2)=='EG'){
              Expected_Gift__c Egifts = new Expected_Gift__c();
              Egifts= [select id from Expected_Gift__c where Unique_Identifier__c= : IdfromSubject];
              
              System.debug('Expected Gift Id*******' + Egifts.Id);
              newTask.add(new Task(Description = email.plainTextBody,
              Priority = 'Normal',           
              Subject = email.subject,
              IsReminderSet = true,           
              WhatId = Egifts.Id));              
          }                              
          try{
          insert newTask;
          }
          catch (System.QueryException e) {
              System.debug('***Could not be inserted....***');
          }
          return result;
      }
  }
*******************************************************************

 
harsha__charsha__c
Hi Sachin,

You have to generate an email instance and an email envelope in your test class and then call the method from your email service handler.

Here (https://developer.salesforce.com/forums/ForumsMain?id=906F000000093uDIAQ) is an example of it.

- Harsha
Sachin Singh 1Sachin Singh 1
Hi Harsh,

Thanks for your response, I started this but the field Unique_Identifier__c is of Type Auto Number, so i can not insert the test data for Custom objects to cover the SOQL code in my class. 

My test class is
*************************
@isTEST
Private class TestMyemailService{
    static testMethod void testCreateContactFrmEmail() {
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
        

        Unclaimed_Gifts__c   UG = new Unclaimed_Gifts__c   (Unique_Identifier__c='UG-000001');
        insert UG;        
        
        Expected_Gift__c EG = new Expected_Gift__c (Unique_Identifier__c='UG-000001');
        insert EG;        
        

        email.subject = 'Create Contact UG-000001';
        email.plainTextBody = 'FromEmail';
        env.fromAddress = 'ilovenagpur@gmail.com';

        MyemailService creatC = new MyemailService ();
        creatC.handleInboundEmail(email, env );
    }
}
***********************
Please help.
harsha__charsha__c
A small tweak to your code gets the job done.

Follow this :
 
@isTEST
Private class TestMyemailService{
    static testMethod void testCreateContactFrmEmail() {
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
        

        Unclaimed_Gifts__c   UG = new Unclaimed_Gifts__c   ();
        insert UG;        
        
        Expected_Gift__c EG = new Expected_Gift__c ();
        insert EG;        
        
        UG = [Select Unique_Identifier__c From Unclaimed_Gifts__c Where Id =: UG.Id];
        email.subject = 'Create Contact ' + UG.Unique_Identifier__c;
        email.plainTextBody = 'FromEmail';
        env.fromAddress = 'ilovenagpur@gmail.com';

        MyemailService creatC = new MyemailService ();
        creatC.handleInboundEmail(email, env );

	EG = [Select Unique_Identifier__c From Expected_Gift__c Where Id =: EG.Id];
        email.subject = 'Create Contact ' + EG.Unique_Identifier__c;
        email.plainTextBody = 'FromEmail';
        env.fromAddress = 'ilovenagpur@gmail.com';

	creatC = new MyemailService ();
        creatC.handleInboundEmail(email, env );

    }
}

This wll do the job for you..Feel free to reach me in case if this does not work...

- Harsha