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
Gopal ChatGopal Chat 

I'm not able to write test class for my class

Class -----------

global class MitratechCustomerSupport implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); 
        String threadIdentifire=email.FromAddress+'--'+email.subject+'--'+email.toAddresses;
        String caseNum=email.subject.subString(email.subject.lastindexOf('-')+1,email.subject.Length());
        String sourceIdStore='';
        String s= ''; 
        Case objCase = new Case();
        EmailMessage objEmailMessage = new EmailMessage();
        Messaging.InboundEmail.Header[] hd=email.headers;
            for(Messaging.InboundEmail.Header hdValue: hd){
                s = s+String.valueOf(hdValue);
        Id caseId=[SELECT Id FROM Case WHERE CaseNumber=:caseNum.trim()][0].Id;
        if( caseId!=NULL){
                    //Verify OTP
                    if(s.contains('Forwarding Confirmation')){
                        objCase.Subject = email.subject;
                        objCase.Origin = 'Email';
                        objCase.Status = 'New';
                        Insert objCase;
                        //Insert Inbound Email Message
                        objEmailMessage.ToAddress =  String.join( email.toAddresses, ',');
                        objEmailMessage.FromAddress = email.FromAddress;
                        objEmailMessage.FromName = email.FromName;
                        objEmailMessage.Subject = email.subject;
                        objEmailMessage.HtmlBody = email.htmlBody;
                        objEmailMessage.Incoming= true;
                        objEmailMessage.TextBody = email.plainTextBody;
                        objEmailMessage.RelatedToId =caseId;
                        insert objEmailMessage;
                        for(Case cs:[SELECT SourceId FROM Case WHERE id=:caseId]){
                            objCase.SourceId =objEmailMessage.Id;
                        update objCase;
                Catch(Exception ex){
                    system.debug('Exception Message'+ex.getMessage()); 
        result.success = true;       
        return result;


Test Class-----

public class MitratechCustomerSupportTest {
    public static void runTest(){
       Case c = new Case(subject = 'TEST',Status ='New', Priority = 'Medium', Origin = 'Email');
       insert c;
      c = [select id, casenumber from Case where id =];
      Messaging.InboundEmail email = new Messaging.InboundEmail() ;
      Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
      email.subject = 'blah blah blah '+'-'+c.casenumber;
      email.fromAddress = '';
      email.plainTextBody = 'email body\n2225256325\nTitle';
      MitratechCustomerSupport  testInbound=new MitratechCustomerSupport ();
      testInbound.handleInboundEmail(email, env);

PriyaPriya (Salesforce Developers) 
Hey Gopal,

Can you kindly describe the issue you are getting after wrting the test class?