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
Navya sree 4Navya sree 4 

Test Class coverage for Help?

Hi
Can anyone help me with test class

global class CreateTaskOnContactFromEmail implements Messaging.InboundEmailHandler {
    
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
        
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        String myPlainText= '';
        string sub = '';
        List<Attachment> attachments = new List<Attachment>();
        String docUrl = '';
        
       
        myPlainText = email.plainTextBody;
        sub = email.subject;
       
        
        sub.contains('ref:');
        Task newTask = new Task();
        Account vAcc = new Account();
        EmailMessage  emailMsg = new EmailMessage();
               try {
            vAcc = [SELECT Id, Name, PersonEmail,OwnerId,Owner.Name FROM Account WHERE PersonEmail =:email.fromAddress AND IsPersonAccount = True
                    LIMIT 1]; 
            Contact vCon = [SELECT Id, Name, Email FROM Contact WHERE Email = :email.fromAddress LIMIT 1];
            
            if(vAcc.id != NULL){
                
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.toAddresses = new String[]{vAcc.OwnerId};
                    message.subject = 'Task is Created for your Account';
                String emailBody = 'Hi'+' '+vAcc.Owner.Name+'\n'+'A new Task is created for your Account'+' '+vAcc.Name;
                message.plainTextBody = emailBody;
                Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{message});
                
                
                if(email.textAttachments != null)
                {
                    for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
                        
                        String body = tAttachment.body;
                        Blob b = blob.valueOf(tAttachment.body);
                        String str = EncodingUtil.base64Encode(b);
                        
                        ContentVersion conVer = new ContentVersion();
                        conVer.VersionData = EncodingUtil.base64Decode(str);
                        conVer.Title = tAttachment.fileName;
                        conVer.ContentLocation = 'S'; 
                        conVer.PathOnClient = tAttachment.fileName;
                        insert conVer;
                        
                        conVer = [select ContentDocumentId from ContentVersion where id = :conVer.id limit 1];
                        
                        ContentDocumentLink cdl = new ContentDocumentLink();
                        cdl.LinkedEntityId = vAcc.id;
                        cdl.ContentDocumentId = conVer.ContentDocumentId;
                        cdl.ShareType = 'I'; 
                        cdl.Visibility = 'InternalUsers';
                        insert cdl; 
                        
                        
                        docUrl += tAttachment.fileName + '\n';
                    }
                }
                if(email.binaryAttachments != null)
                {
                    for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
                        ContentVersion conVer = new ContentVersion();
                        conVer.VersionData = bAttachment.body;
                        conVer.Title = bAttachment.fileName;
                        conVer.ContentLocation = 'S'; 
                        conVer.PathOnClient = bAttachment.fileName;
                        insert conVer;
                        
                        conVer = [select ContentDocumentId from ContentVersion where id = :conVer.id limit 1];
                        
                        ContentDocumentLink cdl = new ContentDocumentLink();
                        cdl.LinkedEntityId = vAcc.id;
                        cdl.ContentDocumentId = conVer.ContentDocumentId;
                        cdl.ShareType = 'I'; 
                        cdl.Visibility = 'InternalUsers';
                        insert cdl;
                        docUrl += bAttachment.fileName + '\n';
                    }
                }
                if(attachments.size() > 0){insert attachments;
                                           system.debug(attachments[0]);}
                
                
                emailMsg.RelatedToId =vAcc.Id;
                emailMsg.TextBody = email.plainTextBody;
                emailMsg.Subject = email.subject;
                emailMsg.FromAddress = email.fromAddress;
                emailMsg.Status = '2';
                emailMsg.Incoming= True;
                emailMsg.Attachment__c = docUrl;
                insert emailMsg;
                
            }
        }catch (QueryException e) {
            UTIL_LoggingService.logHandledException(e, UserInfo.getOrganizationId(),UserInfo.getOrganizationName(),'CreateTaskOnContactFromEmail',
                                                    'handleInboundEmail','', system.LoggingLevel.FINEST); 
        }
        
       
        result.success = true;
        return result;
    }
}

Thanks
Raj VakatiRaj Vakati
try this code 
 
@isTest
private class CreateTaskOnContactFromEmailTest
{
    static testMethod void testUnsubscribe() 
    {
        // Create a new email and envelope object.    
        
 
        
        Account testAccount = new Account();
        testAccount.Name='Test Account' ;
        insert testAccount;
        
        Contact cont = new Contact();
        cont.FirstName='Test';
        cont.LastName='Test';
        cont.Email='test@test.com';
        cont.Accountid= testAccount.id;
        insert cont;
        
        
        
        
        //env.fromAddress = 'test@test.com';
        
         // create a new email and envelope object
       Messaging.InboundEmail email = new Messaging.InboundEmail() ;
       Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
      
       // setup the data for the email
      email.subject = 'Create Contact';
      email.fromAddress = 'test@test.com';
      email.plainTextBody = 'email body\n2225256325\nTitle';
     
      // add an Binary attachment

      Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
      attachment.body = blob.valueOf('my attachment text');
      attachment.fileName = 'textfileone.txt';
      attachment.mimeTypeSubType = 'text/plain';
      email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };

 
      // add an Text atatchment
 
      Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
      attachmenttext.body = 'my attachment text';
      attachmenttext.fileName = 'textfiletwo3.txt';
      attachmenttext.mimeTypeSubType = 'texttwo/plain';
      email.textAttachments =   new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
     
          
        CreateTaskOnContactFromEmail obj= new CreateTaskOnContactFromEmail();
        obj.handleInboundEmail(email, env );
        
    }
    
}

 
Navya sree 4Navya sree 4
Hi raj,
It is giving only 30% coverage only

Thanks