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
Jonas MyhreJonas Myhre 

Need some help for a test class

Hello! I've written an email service which i need to test the code coverage for, and increase it. Its currently at 62%, and needs to be minimun 75%.  Im stuck, can anyone help me, or point me in the right direction? Here is the code, i appreciate help extremely much! : 

global class EmailToLead implements Messaging.InboundEmailHandler {
 
        global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
            Messaging.InboundEnvelope envelope) {
 
            Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
 
             Lead lead = new Lead();
            lead.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
            lead.LastName = email.fromname.substring(email.fromname.indexOf(' '));
            lead.Email = envelope.fromAddress;
            lead.Title = email.subject;
            lead.LeadSource = 'Market Intel';
            String textBody = email.plainTextBody;
            lead.Description = textBody;
            String companyName;
            if(textBody.indexOf('CompanyName:') > -1) {
                Integer startPos = textBody.indexOf('CompanyName:');
                Integer endPos = textBody.indexOf('.',textBody.indexOf('CompanyName:'));
                companyName = textBody.substring(startPos+'CompanyName:'.length(),endPos);
            }
            
            lead.Company = companyName != NULL ? companyName : '(not provided)';
            lead.Status = 'Web';
            insert lead;
     
            if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
            for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
            Attachment attachment = new Attachment();
            // attach to the newly created contact record
            attachment.ParentId = lead.Id;
            attachment.Name = email.binaryAttachments[i].filename;
            attachment.Body = email.binaryAttachments[i].body;
            insert attachment;
          }
    }
    return result;
}
}
Best Answer chosen by Jonas Myhre
pconpcon
So, you have a lot of work to do. Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:
  1. Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  2. Starting the test. This is calling Test.startTest() to reset the governor limits
  3. Calling your class / method
  4. Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  5. Asserting that your changes have worked
    1. If you have inserted/updated/deleted data, you need to query for the updates
    2. Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back

In your case, the setup will consist of building a new InboundEmail object​​ and then passing that into your method

If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/

All Answers

pconpcon
Can you please include your current tests?  This will help to know what corner cases you have not address.

NOTE: Please use the "Add a code sample" button (icon of <>) to increase readability
Jonas MyhreJonas Myhre
This i  the test i've used so far:
 
@isTest
private class SendmailTest{
  @isTest
  static void myTest1() {
    EmailToLead email2lead= new EmailToLead();
  }
}

 
pconpcon
So, you have a lot of work to do. Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:
  1. Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  2. Starting the test. This is calling Test.startTest() to reset the governor limits
  3. Calling your class / method
  4. Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  5. Asserting that your changes have worked
    1. If you have inserted/updated/deleted data, you need to query for the updates
    2. Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back

In your case, the setup will consist of building a new InboundEmail object​​ and then passing that into your method

If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
This was selected as the best answer
Jonas MyhreJonas Myhre
Thanks alot, i will give it a shot! :)