2

my trigger

trigger EmailTest on Opportunity (before insert) {
id ac;
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for (Opportunity opp : trigger.new){
    if (opp.AccountId != null){
        ac = opp.AccountId;
    }
}
for(event e : [Select id, subject, startdatetime from event where AccountId = :ac]){
    if(e.Subject == 'call' || e.StartDateTime >= date.today()){
        for(opportunity opp : trigger.new){
            User u = [SELECT Id, email, name FROM User WHERE id = :opp.OwnerId];
            system.debug('it worked');
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();
            sendTo.add(u.Email);
            mail.setToAddresses(sendTo);
            mail.setReplyTo('[email protected]');
            mail.setSenderDisplayName('Zachary Edgell');
            mail.setSubject('Email Alert for opportunity name ' + opp.name);
            String body = 'Dear ' + u.Name + ', ' + '<br>' + '<br>';
            body += '  You have a call that is due on ' + e.StartDateTime;
            body += ' for ' + opp.Name;
            body += '<br>' + '<br>';
            body += 'Thanks, ' + '<br>';
            body += 'Zachary Edgell';
            mail.setHtmlBody(body);
            mails.add(mail);
            Messaging.sendEmail(mails);
          }
      }
  }
}

my test class

@isTest
public class EmailTestTriggerTest {
    @isTest static void emailTestTrigger() {
        //test opportunity with a account with a call event
        opportunity opp = new opportunity();
        opp.AccountId = '0011U00000ni1fqQAA';
        opp.Name = 'test class 1';
        opp.StageName = 'prospecting';
        opp.CloseDate = date.today();
        insert opp;
        //test opportunity with a account with no event
        opportunity opp1 = new opportunity();
        opp1.AccountId = '0011U00000mLfjzQAC';
        opp1.Name = 'test class no event';
        opp1.StageName = 'prospecting';
        opp1.CloseDate = date.today();
        insert opp1;
        //test opportunity with no account
        opportunity opp2 = new opportunity();
        opp2.Name = 'test class no account';
        opp2.StageName = 'prospecting';
        opp2.CloseDate = date.today();
        insert opp2;
        //test opportunity with account with email event
        opportunity opp3 = new opportunity();
        opp3.AccountId = '0011U00000ni1esQAA';
        opp3.Name = 'test class email event';
        opp3.StageName = 'prospecting';
        opp3.CloseDate = date.today();
        insert opp3;
        //test opportunity with account with meeting event
        opportunity opp4 = new opportunity();
        opp4.AccountId = '0011U00000ni1etQAA';
        opp4.Name = 'test class with meeting event';
        opp4.StageName = 'prospecting';
        opp4.CloseDate = date.today();
        insert opp4;
        //test opportunity with account with send letter event
        opportunity opp5 = new opportunity();
        opp5.AccountId = '0011U00000ni1fCQAQ';
        opp5.Name = 'test class with send letter event';
        opp5.StageName = 'prospecting';
        opp5.CloseDate = date.today();
        insert opp5;
        //test opportunity with account with other event
        opportunity opp6 = new opportunity();
        opp6.AccountId = '0011U00000ni1fMQAQ';
        opp6.Name = 'test class with other event';
        opp6.StageName = 'prospecting';
        opp6.CloseDate = date.today();
        insert opp6;
    }
} 

the only part of the trigger that is being tested is

trigger EmailTest on Opportunity (before insert) {
id ac;
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for (Opportunity opp : trigger.new){
    if (opp.AccountId != null){
        ac = opp.AccountId;
    }
}
for(event e : [Select id, subject, startdatetime from event where AccountId = :ac]){

I am unsure why it is not testing all the code as I created a account with a call event for today and then included id in opp.Accountid = ''

1 Answer 1

2

I am unsure why it is not testing all the code as I created a account with a call event for today and then included id in opp.Accountid = ''

Did you though? In your test class, I only see that you're creating Opportunity records. You're also hard-coding Account Ids. Hard-coding Ids will come back to haunt you if you don't get rid of them now, because the Ids of object records (Account, Opportunity, Case, etc...) will change between environments (the exception being records in full-copy or partial-copy sandboxes taken from prod).

The thing about test classes is that you're expected to create all of your test data inside of your test class...unless you're using seeAllData=true, which you generally shouldn't be.

Since you're not creating any Account or Event records, when your trigger reaches that point in its execution, your query returns 0 records (in the context of the unit test) and you skip over the loop in its entirety.

The basic order of operations in unit testing is:

  1. Create all of the required data so that the code you want to run actually can be run
  2. Execute the code you want to be tested
  3. Gather the results, and make assertions to verify that your code does what you think it should be doing

Creating the Account(s) and Event(s) falls under step 1 here.
Your existing unit test already takes care of #2
You should also add some assertions to take care of #3. The general rule is if you change the publicly visible state of a class, or add/update/delete records, you should assert that that happened as you intended

2
  • 1
    Also, something other people are bound to bring up if I don't (but I don't feel it's the focus of the question or my answer). It's good practice to keep the logic of your trigger outside of the trigger itself. I believe Chris Aldridge's Lightwieght Trigger Framework would be a good starting point, and he goes into detail about why this is a good idea.
    – Derek F
    Aug 4, 2019 at 22:25
  • That worked perfect I appreciate the help. Aug 4, 2019 at 22:36

You must log in to answer this question.

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