0

I'm trying to write a unit test for a simple trigger so I can have 100% code coverage.

Here's my trigger:

    // When a new lead is created this trigger copies the Lead Score and Lead Grade from Pardot
    trigger LeadScoreAtLeadCreation on Lead (before insert) {
        LeadScoreTriggerHandler handler = new LeadScoreTriggerHandler();

        if(Trigger.isInsert && Trigger.isBefore) {
            handler.setPardotScoreEqualToLeadScore(Trigger.new);
        }
    }

I wrote some test code already but the developer console isn't picking up on it.

@isTest
public class testLeadScoreAtLeadCreation {
    @isTest static void testLeadScoreSetter() {
        LeadScoreTriggerHandler handler = new LeadScoreTriggerHandler();

        List<Lead> aryLead = new List<Lead>();

        Lead testlead = new Lead(pi__score__c=50.0, Created_Lead_Score__c=null);

        aryLead.add(testlead);

        Test.startTest();
        handler.setPardotScoreEqualToLeadScore(aryLead);
        Test.stopTest();

        System.assertEquals(50.0, testlead.Created_Lead_Score__c);
    }
}
  • You never insert the lead, insert testlead – EricSSH Aug 15 '17 at 15:34
  • 2
    If you want to test the trigger, you need the trigger to actually run. In other words, insert a Lead. – Adrian Larson Aug 15 '17 at 15:34
  • I would suggest, you do not put much logic in the trigger class itself. But rather create a TriggerHandler class with that logic and kick that off from trigger. Now you can test this class independent of trigger cycle and also reuse the logic. – realfire Aug 15 '17 at 15:40
1

You have to actually insert lead, you should also bulk test this considering how many times this trigger is going to kick off. One thing I would also consider is a qualifier on your trigger if possible, every time a Lead gets inserted the Lead trigger is going to do this processing every time, which you may not want.. Just something to think about.

There is also no reason to call your public method, your trigger will do that. Although sometimes I do like to simply test my public methods, but those are in certain circumstances. But really the only thing you weren't doing was you were never insert aryLead.

As for the asserts, I will assert that all my expected data got inserted along with the expected results

public class testLeadScoreAtLeadCreation 
{
    @isTest static void testLeadScoreSetter() {
    LeadScoreTriggerHandler handler = new LeadScoreTriggerHandler();

    List<Lead> aryLead = new List<Lead>();

    for(Integer i = 0; i < 200; i++)
    {
        Lead testlead = new Lead(pi__score__c=50.0, Created_Lead_Score__c=null);
        aryLead.add(testlead);
    }

    Test.startTest();

        insert aryLead;

    Test.stopTest();

    List<Lead> testResult = [SELECT ID, pi__score__c, Created_Lead_Score__c FROM Lead];

    System.assertEquals(200, testResult.size());

    for(Lead l : testResult)
    {
         System.assertEquals(50.0, l.Created_Lead_Score__c);
    } 
}
-1

Unless you insert the Lead the trigger will not execute. To get the 100% code coverage Lead should be inserted.

Your updated test class should be like this below

@isTest
public class testLeadScoreAtLeadCreation {
@isTest static void testLeadScoreSetter() {
    LeadScoreTriggerHandler handler = new LeadScoreTriggerHandler();

    List<Lead> aryLead = new List<Lead>();

    Lead testlead = new Lead(pi__score__c=50.0, Created_Lead_Score__c=null);

    aryLead.add(testlead);

    // aryLead inserted
    insert aryLead;

    Test.startTest();
    handler.setPardotScoreEqualToLeadScore(aryLead);
    Test.stopTest();

    System.assertEquals(50.0, testlead.Created_Lead_Score__c);
}
}
  • 3
    Please use words to explain what should be changed, not just code. – Adrian Larson Aug 15 '17 at 15:40
  • Okay, as you said earlier. Unless you insert the Lead the trigger will not execute. To get the 100% code coverage Lead should be inserted. – Firoj Mahmud Aug 15 '17 at 15:41
  • Please edit your answer to be more clear. That is not what the comments are for. I am not the one who voted down your answer, but these things may contribute to your post being poorly received. – Adrian Larson Aug 15 '17 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.