0

I have the following class and I'm trying to get code coverage for it, but so far it's at 0%. I have pretty limited experience with Apex, so would really appreciate the help!

public with sharing class customerEndDate2Days {

@InvocableMethod

public static void sendEmail(List<Account> acc) {

String sourceAccountId = String.valueOf(acc[0].Id);
List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
List<Contact> contactsToEmail = [SELECT Id, Name
                                 FROM   Contact
                                 WHERE  (RADAR_Administrator__c = 'Customer Admin'
                                 OR RADAR_Administrator__c = 'Parent Admin')
                                 AND AccountId = :sourceAccountId];
                                 System.debug('contactsToEmail = ' + contactsToEmail);

Id templateId = [SELECT Id, Name 
                 FROM   EmailTemplate 
                 WHERE  Name = 'End of Contract 2 days Alert'].Id;
                 System.debug('Email Template = ' + templateId);

for(Contact con: contactsToEmail ){

  Messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
  email.setTemplateId(templateId);
  email.setTargetObjectId(con.Id);
  email.setSaveAsActivity(true); 
  email.setSenderDisplayName('Display Name Here'); 
  emailsToSend.add(email);

}
Messaging.sendEmail(emailsToSend);   }}

And here is my attempt at the test class:

@isTest    
Global class CustomerEndDateDaysTest {

public static void sendEmail(ID recipient, ID candidate) {
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   mail.setTargetObjectId(recipient);
   mail.setTemplateId('00X8E000000VDde');
   mail.setWhatId(candidate);   
   mail.setBccSender(false);
   mail.setUseSignature(false);
   mail.setSaveAsActivity(false); 
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    } 

    public static testmethod void testvalidate(){

    Account newAcc = new Account();
    newAcc.name='test';
    insert newAcc;

    Contact con=new Contact();
    con.lastname='Testing';
    con.email='[email protected]';
    con.RADAR_Administrator__c = 'Customer Admin';
    insert con;

    sendEmail(con.Id,newAcc.Id);
}
}

What am I missing here? Thanks!

2
  • Fundamentally, your test class needs to execute your main class, usually by calling it. Since your main class isn't executed, you're not testing it.
    – David Reed
    Jul 31, 2018 at 10:37
  • There are some very good modules on testing on Trailhead, as well as a superb blog series on developer.salesforce.com. I recommend taking a look and giving this another shot.
    – David Reed
    Jul 31, 2018 at 10:38

2 Answers 2

2

You are missing a couple of points

  1. You need to Link the Contact with the Account while you are inserting the Contact in Test Class.
  2. Call the SendEmail method of the Class that you created "customerEndDate2Days".

Use below code for the test class

@isTest    
Global class CustomerEndDateDaysTest {
    public static testmethod void testvalidate(){
        Account newAcc = new Account();
        newAcc.name='test';
        insert newAcc;

        Contact con=new Contact();
        con.lastname='Testing';
        con.email='[email protected]';
        con.AccountId = newAcc.Id; // Link Contact to the Account
        con.RADAR_Administrator__c = 'Customer Admin';
        insert con;
        Test.startTest();
            customerEndDate2Days.sendEmail(new List<Account>{newAcc});
        Test.stopTest();
    }
}

Refer the below Link for the best practice Testing Best Practices

1
  • 2
    Thanks @Amit ! I'll def give that a read and also do the trailhead, David :)
    – Priscila
    Jul 31, 2018 at 11:06
-1

No need to write send email logic in the test class. only thing is, you have to create the contact record with the exact value which is mentioned in the soql condition. then only it will get the contact records in apex while running this test class.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .