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

Test class for Email Service apex class

I want to write test class for this Email Service apex class

global class ResourceToLeave implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
                                                       Messaging.InboundEnvelope env){

    // Create an InboundEmailResult object for returning the result of the
    // Apex Email Service
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    String[] emailBody = email.plainTextBody.split('\n', 0);
    String startdate = emailBody[0].substring(10);
    String[] tempStr = startdate.split('/');
    Integer d = Integer.valueOf(tempStr[0]);
    Integer m = Integer.valueOf(tempStr[1]);
    Integer y = Integer.valueOf(tempStr[2]);
    Date sdt = Date.newInstance(y,m,d);
    String enddate = emailBody[1].substring(8);
    String[] tempStr1 = enddate.split('/');
    Integer dd = Integer.valueOf(tempStr1[0]);
    Integer mm = Integer.valueOf(tempStr1[1]);
    Integer yy = Integer.valueOf(tempStr1[2]);
    Date edt = Date.newInstance(yy,mm,dd);
    // New Leave object to be created
    Leave__c[] leave = new Leave__c[0];
    // Try to look up any contacts based on the email from address
    // If there is more than one contact with the same email address,
    // an exception will be thrown and the catch statement will be called.
    try {
      Resource__c[] resource = [SELECT Id, Name FROM Resource__c WHERE Name = :email.subject LIMIT 1];
      Resource__c res;
      // Add a new Task to the contact record we just found above.
      leave.add(new Leave__c(Resource__c=res.Id,
     // Insert the new Leave
     insert leave;   
     System.debug('New Leave Object: ' + leave );  
    // If an exception occurs when the query accesses
    // the contact record, a QueryException is called.
    // The exception is written to the Apex debug log.
   catch (System.QueryException e) {
       System.debug('Query Issue: ' + e);
   // Set the result to true. No need to send an email back to the user
   // with an error message
   result.success = true;
   // Return the result for the Apex Email Service
   return result;
How far have you got so far?  Do you have a test class to share with us?
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
If you haven't started yet, this should get you started:
Thanks for replying.. But i'm not getting any help from these links..

I have written few lines please help me completing this test class..
Please tell me how to test that Resource__c object and Leave__c object in apex class..

public class ResourceLeaveTest
    public static testMethod void ResourceLeave()
        // Create a new email and envelope object
        Messaging.InboundEmail email  = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        // Set up your data if you need to
        // Create the email body
        email.plainTextBody = 'StartDate:01/01/2014\nEndDate:05/01/2014';
        email.fromAddress ='';
        email.subject = 'Dummy Account Name 123';
        ResourceToLeave catcher = new ResourceToLeave();
        Messaging.InboundEmailResult result = catcher.handleInboundEmail(email, env);
        System.assertEquals( result.success  ,true); 
Damien Phillippi033905702927186443Damien Phillippi033905702927186443
In your test class, insert a Resource__c object into the database that shares a name of the subject you are using.