function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sam WardSam Ward 

test class attempt

Hi,

So I have the below code and I know it works okay 
 
global class MarketingClicks implements Schedulable {
    global void execute(SchedulableContext ctx) {
        List<Lead> Leadlist = [SELECT Id, Campaign_Name__c FROM Lead WHERE Campaign_Name__c != ''];

  if(!LeadList.isEmpty()){
        for(Lead L : LeadList)
        {        
            //Create Marketing Click
            L.Link_Type__c = 'TEST';
            Pardot_Campaign__c PC = new Pardot_Campaign__c();
            PC.Lead__c = L.Id;
            PC.Name = L.Campaign_Name__c;
            insert PC;
            L.Campaign_Name__c= '';
            Update L;
            }
        }
        System.debug(Leadlist);
    }
    
}

I have also added in 4 schedules which trigger this: 
 
System.schedule('Marketing Clicks Job 1', '0 0 * * * ?', new MarketingClicks ());
System.schedule('Marketing Clicks Job 2', '0 15 * * * ?', new MarketingClicks ());
System.schedule('Marketing Clicks Job 3', '0 30 * * * ?', new MarketingClicks ());
System.schedule('Marketing Clicks Job 4', '0 45 * * * ?', new MarketingClicks ());
Again this works fine from what i'm seeing. 

I've then tried to write a test class by following a video and changing the fields that I need instead of what the video was using and it doesn't work can someone help please? 

Test Class: 
@isTest
Public Class MarketingClicksTest
{
  @testSetup
    static void setup()
    {
        List<Lead> Leadlist = new List<Lead>();
        for(Integer i=1;i<=10;i++)
        {
         Lead Id = new Lead(Company='Test',Lastname='sam',Status='Open',UK_Canada__c='UK',Phone='000000',Online_Offline__c='Offline',Enquiry_Source__c='Cold Call',Method__c='N/A',Position__c='UNASSIGNED',Industry_Sector__c='Miscellaneous',Campaign_Name__c='Test');
        }
        insert LeadList;
    }
    
    static testmethod void testMarketingClicksScheduleJob()
    {
        string sch='0 5 12 10 2 ?';
        Test.startTest();
        string jobId=system.schedule('scheduleApexTest',sch,new MarketingClicks());
        List<Lead> LeadList=[SELECT Id, Campaign_Name__c FROM Lead WHERE Campaign_Name__c != ''];
        system.assertEquals(10, Leadlist.size());
        Test.stopTest();
    }
}


Thanks in advance
Raffaele MessinaRaffaele Messina
Maybe the problem is that you need to test also the execute() method of your Schedulable class
David Zhu 🔥David Zhu 🔥
You may use the code snippet below:
static testmethod void testMarketingClicksScheduleJob()
    {
        string sch='0 5 12 10 2 ?';
        Test.startTest();
        string jobId=system.schedule('scheduleApexTest',sch,new MarketingClicks());

      // Get the information from the CronTrigger API object
      CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered FROM CronTrigger WHERE id = :jobId];

      // Verify the expressions are the same
      System.assertEquals(sch, ct.CronExpression);

      // Verify the job has not run
      System.assertEquals(0, ct.TimesTriggered);

        Test.stopTest();  // Now that the scheduled job has executed,

        List<Lead> LeadList=[SELECT Id, Campaign_Name__c FROM Lead WHERE Campaign_Name__c != ''];
        system.assertEquals(0, Leadlist.size());    //the assertion should equal to 0, based on line 14 L.Campaign_Name__c= ''; in execute method of MarketingClicks.

         List<Pardot_Campaign__c> pcs = [Select Id from Pardot_Campaign__c];
         system.assertEquals(10,pcs.size());
    }

 A side note of execute method, Salesforce suggests not to use DML in loop. You may need to enhance execute method.