+ Start a Discussion
ManidManid 

Test class for the inboundemail

global class inboundEmailattachment implements messaging.inboundemailhandler {
    global messaging.inboundemailresult handleinboundemail(messaging.inboundemail email,messaging.inboundEnvelope evn){
        messaging.inboundemailresult res=new messaging.inboundemailresult();
        contact con=[select lastname,email from contact where email=:email.fromaddress];
        case c=new case();
        c.Subject=email.subject;
        c.Description=email.plaintextbody;
        c.origin='web';
        c.status='new';
        c.priority='high';
        c.contactid=con.id;
        insert c;
        messaging.inboundemail.binaryattachment[] attach=email.binaryattachments;
        list<attachment> mylist=new list<attachment>();
        for(messaging.inboundemail.binaryattachment b:attach){
            attachment a=new attachment();
            a.name=b.filename;
            a.Body=b.body;
            a.parentid=c.id;
            mylist.add(a);
        }
        insert mylist;
        return res;
    }
}
for this i write test class but it not cover all lines of code
test class:
@istest
public class inboundemailattachmentTest {
    
   static testMethod void TestinBoundEmail()
   {
       Messaging.InboundEmail email = new Messaging.InboundEmail() ;
       Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
      
      email.subject = 'Create Contact';
      email.fromAddress = 'someaddress@email.com';
      email.plainTextBody = 'email body';
      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 };
     
      inboundEmailattachment  testInbound=new inboundEmailattachment ();
      testInbound.handleInboundEmail(email, env);
   }
}
please teach me test class 
 
Best Answer chosen by Manid
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi Manid,

To conver apex code coverage, you have to create some test data in your test class. 

Please use thre updated test class below. This will give 100% coverage.
@istest
public class inboundemailattachmentTest {
    
   static testMethod void TestinBoundEmail()
   
   {
       Contact Con = new Contact();
       con.LastName = 'sample';
       con.Email = 'someaddress@email.com';
       Insert con;
       Messaging.InboundEmail email = new Messaging.InboundEmail() ;
       Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
      
      email.subject = 'Create Contact';
      email.fromAddress = 'someaddress@email.com';
      email.plainTextBody = 'email body';
      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 };
     
      inboundEmailattachment  testInbound=new inboundEmailattachment ();
      testInbound.handleInboundEmail(email, env);
   }
}

As Sudipta Deb mentioned go through trial head to learn about test classes.

Mark this as a best answer if you find this is helpfull.

Thanks

All Answers

Sudipta DebSudipta Deb
Hi,

You need to complete the Trailhead module to learn Apex Testing. It is one of the important steps of your development process.
https://trailhead.salesforce.com/en/modules/apex_testing

Once you are done with the above module, you will understand the importance of test classes, test coverage and how to write test classes in Apex.
You can go through the below Salesforce Developer Documentation as well.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

Please accept this as best answer if my reply is helpful.
Naveen Kumar B H(bhns)Naveen Kumar B H(bhns)
Hi Manid,

To conver apex code coverage, you have to create some test data in your test class. 

Please use thre updated test class below. This will give 100% coverage.
@istest
public class inboundemailattachmentTest {
    
   static testMethod void TestinBoundEmail()
   
   {
       Contact Con = new Contact();
       con.LastName = 'sample';
       con.Email = 'someaddress@email.com';
       Insert con;
       Messaging.InboundEmail email = new Messaging.InboundEmail() ;
       Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
      
      email.subject = 'Create Contact';
      email.fromAddress = 'someaddress@email.com';
      email.plainTextBody = 'email body';
      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 };
     
      inboundEmailattachment  testInbound=new inboundEmailattachment ();
      testInbound.handleInboundEmail(email, env);
   }
}

As Sudipta Deb mentioned go through trial head to learn about test classes.

Mark this as a best answer if you find this is helpfull.

Thanks
This was selected as the best answer
ManidManid
thanks bro.