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
Nicole Young 2Nicole Young 2 

I need help writing an apex test class but not sure where to start. Any help would be greatly appreciated!

global class CreateLeadEmailService implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        String leadName=email.fromName;
        String leadDes=email.plaintextbody;
        String leadSub=email.subject;
        String leadEmail=email.fromAddress;
        Lead l=new Lead(LastName=leadName, Email=leadEmail, Subject_del__c=leadSub, Description=leadDes, Company=leadEmail.split('@').get(1).split('\\.').get(0) );
        insert l;
        return result;
    }
}
Best Answer chosen by Nicole Young 2
PawanKumarPawanKumar
Hi Nicole,
Please try below test code.

@isTest(SeeAllData=false)
public class CreateLeadEmailServiceTest  {
 public static testMethod void testEmailService() 
    {        
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

        email.subject = 'Test Subject';
        email.fromAddress = 'xxx@xxx.com';
        email.plainTextBody = 'Test PBODY';        
        email.fromname='Test Name';
        CreateLeadEmailService testCreateLeadEmailService = new CreateLeadEmailService();
        testCreateLeadEmailService.handleInboundEmail(email, env);
    }
}

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
Hi Nicole,
Please try below test code.

@isTest(SeeAllData=false)
public class CreateLeadEmailServiceTest  {
 public static testMethod void testEmailService() 
    {        
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

        email.subject = 'Test Subject';
        email.fromAddress = 'xxx@xxx.com';
        email.plainTextBody = 'Test PBODY';        
        email.fromname='Test Name';
        CreateLeadEmailService testCreateLeadEmailService = new CreateLeadEmailService();
        testCreateLeadEmailService.handleInboundEmail(email, env);
    }
}

Regards,
Pawan Kumar
This was selected as the best answer
Nicole Young 2Nicole Young 2
Thank you Pawan! That worked!!