0

Working on creating a test class for my class in my sandbox and I am receiving a code coverage 0% in the dev console. Code Coverage None The console isn't highlightning the lines that need to be covered in red either.

Apex Class:

//Process Builder Passes in variables from event
global class InvocableSendEmail {

    global class DataWrapper {
        @InvocableVariable(label='Record Id' required=true)
        public String recordId;
    }

//Invoked Apex sends Email
    @InvocableMethod(label='Send Email')
    global static void sendMail (List<DataWrapper> passedData) {

    //Send Mail
    for (DataWrapper dw: passedData) {

        //#Should rewrite this to pull a list and then query it for the emails sent and received IDs
        //Lookup Remedyforce Action's Id: 'Email Sent'
        Id actionIdEmailSent = [SELECT Id  FROM BMCServiceDesk__Action__c  WHERE Name = 'Email Sent' LIMIT 1].Id;
        system.debug('*** Action Email Sent ID: ' + actionIdEmailSent);

        //Lookup Remedyforce Action's Id: 'Email Received'
        Id actionIdEmailReceived = [SELECT Id FROM BMCServiceDesk__Action__c  WHERE Name = 'Email Received' LIMIT 1].Id;
        system.debug('*** Action Email Received ID: ' + actionIdEmailReceived);


        //Look up most recipients from last email sent or received by the incident
        BMCServiceDesk__IncidentHistory__c lastEmailRecipients =
            [SELECT BMCServiceDesk__EmailConversationData__c
            FROM BMCServiceDesk__IncidentHistory__c
            WHERE BMCServiceDesk__FKIncident__c =: dw.recordId
            AND (BMCServiceDesk__FKAction__c =: actionIdEmailSent
                OR  BMCServiceDesk__FKAction__c =: actionIdEmailReceived)
            ORDER BY BMCServiceDesk__date__c DESC LIMIT 1 ];
        system.debug('*** Email Recipients: ' + lastEmailRecipients);


        String input = ''+lastEmailRecipients; //Converts BMCServiceDesk__EmailConversationData__c to String
        String extractFrom = input.substringBetween('From:' , '#@#@#@#To:');
            system.debug('*** From Recipient: ' + extractFrom);
        String extractTo = input.substringBetween('To:' , '#@#@#@#To:');
            if (String.isBlank(extractTo)) {
                extractTo = '';
            }
            system.debug('*** To Recipients: ' + extractTo);
        String afterDelimiter = 'CC:';
        String extractCC = input.substringAfter(afterDelimiter);  // 'is-test-data'
          if (String.isBlank(extractTo)) {
              extractCC = '';
          }
          system.debug('*** CC Recipients: ' + extractCC);

        //Lookup the Template ID and assign it to variable
        EmailTemplate selectedTemplate = [SELECT Id
                                            FROM EmailTemplate
                                            WHERE Name = 'TEST'];
                                            //replace with template

        Messaging.SingleEmailMessage mailToSend = new Messaging.SingleEmailMessage();

        //Populate Email Processed via Apex with the Variables assigned above
        mailToSend.setTargetObjectId(dw.recordId);
        mailToSend.setTemplateId(selectedTemplate.Id);
        mailToSend.setHtmlBody('TEST'); //update to use notebody

        List<Messaging.SendEmailResult> sendResults = Messaging.sendEmail (new List<Messaging.Email> {mailToSend});
        System.debug(sendResults[0]);

        }
    }
}

Test Class

@isTest //(seealldata=true) --- REMOVED
private class InvocableSendEmailTest {

    // Create Test Incident/Incidnet History
    @isTest static void testAddIncident() {

    BMCServiceDesk__Action__c a = [SELECT Id FROM BMCServiceDesk__Action__c WHERE Name = 'Email Received' LIMIT 1];
    system.assert (a != null);

    BMCServiceDesk__Incident__c i = new BMCServiceDesk__Incident__c(BMCServiceDesk__shortDescription__c='Test subject');
    insert i;

    BMCServiceDesk__IncidentHistory__c ih = new BMCServiceDesk__IncidentHistory__c(BMCServiceDesk__FKIncident__c = i.Id,
        BMCServiceDesk__FKAction__c = a.Id,
        BMCServiceDesk__EmailConversationData__c ='');
    insert ih;

    /*Test.startTest();
        testAddIncident();
        Integer invocations = Limits.getEmailInvocations();
    Test.stopTest();

    system.assertEquals(1, invocations, 'An email should be sent');   
    */ 

    }
}

Apex Test Execution - Store Only Aggregated Code Coverage is unchecked.

Solution:

//specified my class to test    
    List<InvocableSendEmail.DataWrapper> invokedVariables = new List<InvocableSendEmail.DataWrapper>();
    InvocableSendEmail.DataWrapper invVar = new InvocableSendEmail.DataWrapper();
        invVar.recordId = ih.id;
    invokedVariables.add(invVar);

//debug it 
       system.debug('*** InvVar: ' + invVar.recordId);
       system.debug('*** IncidentHistory Name: ' + ih.Name);
       system.debug('*** IncidentId: ' + ih.BMCServiceDesk__FKIncident__c);
       system.debug('*** ActionId: ' + ih.BMCServiceDesk__FKAction__c);

//test it
    Test.startTest();
       InvocableSendEmail.sendMail(invokedVariables);           
       System.assert(invVar.recordId != null);  
        //Assertions to be added
    Test.stopTest();

marked as duplicate by David Reed apex May 27 at 18:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Your unit test is not calling the code it intends to validate. You'll obtain no code coverage on this class for that reason. Please see the linked canonical questions for more resources on writing quality unit tests, including the perils of seeAllData=true. – David Reed May 27 at 18:31
  • Thanks got it resolved. Ditched seeAllData – Gabriel M May 29 at 15:41