+ Start a Discussion
Paula VoglerPaula Vogler 

Help creating a test class to meet minimum code coverage

I am not an apex developer and I am the only SFDC Admin at my company. We are a smaller non-profit org and the directive is to use standard "out of the box" functionality which I can do with my declarative coding knowledge. Probelm is, Admins before me created some triggers and did not create enough test classes for them to meet the minimum code coverage of 75%. This means I cannot deploy any changes via Change Sets, I have to do it all manually which is what i have been doing. Now we have another trigger created by a previous admin that is causing errors for my users due to a change in Roles/permissions that needed to be implemented. Because I cannot de-activate this trigger (which is not even needed, no one seems to know the reason it was created) users will keep getting the errors and I cannot do any further enhancements needed. Salesforce will not help since it's custom code but they did supply me with the classes that do not have enough code coverage. It looks to me like they are already test classes though so I'm not sure if I need to create a new Test class for it or edit the existing. I'm really in a bind and I've exhausted all efforts and knowledge at this point and I'm sure the users as well as my boss will be expecting a resolution. Any insight or assistance would be greatly appreciated. There are 4 classes according to SFDC and here is the code for 1 of them: 
/*
* Tests
*/


/*
* This is the basic test
*/

public with sharing class testSendEmailFlowPlugin {


     public static final String SUBJECT = 'Subject of Test Email';
     public static final String SUBJECT1 = 'Subject of Test Email with Only Email Address';
     public static final String BODY = 'BODY of Test Email';
     public static final String EMAIL_ADDRESS = 'blah@blah.org';
     public static final String TEXT_ATTACHMENT_NAME = 'My Text Attachment';
     public static final String TEXT_ATTACHMENT_BODY = 'My Text Attachment BODY';
     public static final String PDF_ATTACHMENT_NAME = 'My PDF Attachment.pdf';
     public static final String PDF_ATTACHMENT_BODY = 'My PDF Attachment BODY';
     public static final String INVALIDID = '000000000000000';   
    
    static testMethod void basicTest() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='tuser15@salesforce.com');
        insert testLead;
   
      
        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);
       
        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');
       
        Task aTask = [select Subject from Task where WhoID = :testLead.ID];
        System.AssertEquals(aTask.Subject, 'Email: Subject of Test Email');
       
        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
  //      System.assert(aLead.ActivityHistories.size()==1);
  //      System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);
       
    }
   
   
    static testMethod void basicTestwithTextAttachment() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='tuser15@salesforce.com');
        insert testLead;
   
      
        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);
        inputParams.put('textAttachmentName',TEXT_ATTACHMENT_NAME);
        inputParams.put('textAttachmentContent',TEXT_ATTACHMENT_BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');
        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
//        System.assert(aLead.ActivityHistories.size()==1);
//        System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);       
       
        Attachment anAttach = [select id, name from Attachment where parentID = :testLead.ID];
        System.AssertEquals(anAttach.name, TEXT_ATTACHMENT_NAME);
       
    }


    static testMethod void basicTestwithpdfAttachment() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='tuser15@salesforce.com');
        insert testLead;
   
      
        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);
        inputParams.put('pdfAttachmentName',PDF_ATTACHMENT_NAME);
        inputParams.put('pdfAttachmentContent',PDF_ATTACHMENT_BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');
        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
//        System.assert(aLead.ActivityHistories.size()==1);
//        System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);       
       
        Attachment anAttach = [select id, name from Attachment where parentID = :testLead.ID];
        System.AssertEquals(anAttach.name, PDF_ATTACHMENT_NAME);
       
    }
   
/*
  * This test is to test the convert Lead with the Account ID specified
  */
       static testMethod void basicTestwithCCEmail() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='tuser15@salesforce.com');
        insert testLead;
   
      
        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);
        inputParams.put('emailAddress',EMAIL_ADDRESS);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');
                       
        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
//       System.assert(aLead.ActivityHistories.size()==1);
//       System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);
//       System.assertEquals(aLead.ActivityHistories[0].Description.contains(EMAIL_ADDRESS), True);

               
    }

    static testMethod void basicTestwithTextAttachmentandCCEmail() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', Email='tuser15@salesforce.com');
        insert testLead;
      
        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);
        inputParams.put('emailAddress',EMAIL_ADDRESS);
        inputParams.put('textAttachmentName',TEXT_ATTACHMENT_NAME);
        inputParams.put('textAttachmentContent',TEXT_ATTACHMENT_BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');       
   
        Lead aLead = [select name, (SELECT Subject, ActivityDate, Description from ActivityHistories) FROM Lead where id=:testLead.ID];
//       System.assert(aLead.ActivityHistories.size()==1);
//       System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);
//       System.assertEquals(aLead.ActivityHistories[0].Description.contains(EMAIL_ADDRESS), True);
        Attachment anAttach = [select id, name from Attachment where parentID = :testLead.ID];
        System.AssertEquals(anAttach.name, TEXT_ATTACHMENT_NAME);
       
    }

static testMethod void attachmentTest() {

        // Create dummy lead
        Lead testLead = new Lead(Company='Test Lead',FirstName='John',LastName='Doe', email='vrajaram@salesforce.com');
        insert testLead;
           
        // Create dummy conversion
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',testLead.ID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body','testing body');
        inputParams.put('textAttachmentName','textattach');
        inputParams.put('textAttachmentContent','testing text content');
        inputParams.put('pdfAttachmentName','pdfattach');
        inputParams.put('pdfAttachmentContent','testing pdf content');
       


        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);

        System.assertEquals(result.outputparameters.get('Status'),'SUCCESS');
               
        Lead aLead = [select name, (SELECT Subject from ActivityHistories), (select name from Attachments) FROM Lead where id=:testLead.ID];
     //   System.assert(aLead.ActivityHistories.size()==1);
     //   System.assertEquals(aLead.ActivityHistories[0].subject, 'Email: '+SUBJECT);
       
        System.assert(aLead.Attachments.size()==2);
        String attach1Name = aLead.Attachments[0].name;
        String attach2Name = aLead.Attachments[1].name;
       
        System.assert(attach1Name == 'textattach' || attach2Name == 'textattach');
        System.assert(attach1Name == 'pdfattach.pdf' || attach2Name == 'pdfattach.pdf');
       

       

    }



/*
  * -ve Test
  */ 
  static testMethod void negativeTest() {

        // Create dummy lead
   
      
        // Test Sending Email against a record
        SendEmail aSendEmailPlugin = new SendEmail();
        Map<String,Object> inputParams = new Map<String,Object>();
        Map<String,Object> outputParams = new Map<String,Object>();

        inputParams.put('recordID',INVALIDID);
        inputParams.put('subject',SUBJECT);
        inputParams.put('body',BODY);

        Process.PluginRequest request = new Process.PluginRequest(inputParams);
        Process.PluginResult result;
        result = aSendEmailPlugin.invoke(request);
       
        System.assertEquals(result.outputparameters.get('Status'),'ERROR');
       
    } 
 
   
/*
  * This test is to test the describe() method
  */
        static testMethod void describeTest() {

                SendEmail aSendEmailPlugin = new SendEmail();
                Process.PluginDescribeResult result = aSendEmailPlugin.describe();
               
                System.AssertEquals(result.inputParameters.size(), 8);
                System.AssertEquals(result.OutputParameters.size(), 2);
       
        }

It says 0% code coverage but it looks like there's a lot going on there so I'm not sure what needs to be done. Any assistance would be greatly appreciated.
Best Answer chosen by Paula Vogler
ShashForceShashForce
Hi,

2 things here.

First, the first line in a test class must be "@isTest". Without this, it will not be considered as a test class and will count under lines of code which need coverage. Adding this line will make sure that the test class code does not need code coverage. Something like:

/*
* Tests
*/


/*
* This is the basic test
*/

@isTest
public with sharing class testSendEmailFlowPlugin {
.....

Second, Once the tests are written, you should run the tests so that code coverage is calculated. Go to Setup | Develop | Apex Test Execution and run the test classes that you have.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank

All Answers

ShashForceShashForce
Hi,

2 things here.

First, the first line in a test class must be "@isTest". Without this, it will not be considered as a test class and will count under lines of code which need coverage. Adding this line will make sure that the test class code does not need code coverage. Something like:

/*
* Tests
*/


/*
* This is the basic test
*/

@isTest
public with sharing class testSendEmailFlowPlugin {
.....

Second, Once the tests are written, you should run the tests so that code coverage is calculated. Go to Setup | Develop | Apex Test Execution and run the test classes that you have.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
This was selected as the best answer
Paula VoglerPaula Vogler
Thank you so much for that info Shashank. I am a total novice to Test Classes and was not aware of the "@isTest" excluding it from code coverage. I did add that and it increased the code coverage (slightly). Unfortunately there were multiple errors when I ran it though so I'll have to do some more investigating and researching but I'm a step further than I was now. Thanks again!