• Anita Garg
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi Friends,
Please help me on below scenerio, and let me know why the assert is failing. experts please suggest
As per my understanding the LeadSource value is populated in the batch class, but after executing the test class assert is not finding it, eventhough I placed assert after the Test.stopTest() method.

Again by providing the value in the test class, the assert works, is this the correct way to test?
as the value is suppose to be set by the execute method, it seems odd to me to set that value in the test class to pass the test
eg:
l.LeadSource = 'Dreamforce';

LeadProcessor.class
global class LeadProcessor implements Database.Batchable<sObject> {
   
    global Integer count = 0;

    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('SELECT Id, LeadSource FROM Lead');
    }

    global void execute(Database.BatchableContext bc, List<Lead>l_lst){
        List<lead> l_lst_New = new List<Lead>();
        for(Lead l:l_lst){
            l.LeadSource = 'Dreamforce';
            l_lst_New.add(l);
            count = count + 1;
        }
        Update l_lst_New;
    }

    global void finish(Database.BatchableContext bc){
        System.debug('count = '+count);
    }
}

LeadProcessorTest.class
@isTest
public class LeadProcessorTest {
@isTest
    public static void testit(){
        List<Lead> l_lst = new List<Lead>();
        for(Integer i=0;i<200;i++ ){
            Lead l = new Lead();
            l.LastName = 'name'+i;
            l.Company = 'Company';
            l.status = 'Random Status';
            l_lst.add(l);
        }
        INSERT l_lst;

        Test.startTest();
        LeadProcessor lp = new LeadProcessor();
        Database.executeBatch(lp);
        Test.stopTest();
        List<Lead> l_lstX = [SELECT Id, LastName, LeadSource FROM Lead];
        for(Lead l:l_lstX){
            //System.debug('Lead Last Name ' + l.LastName + ' LeadSource='+l.LeadSource);
        }
        System.assertEquals(200, [select count() from Lead where LeadSource = 'Dreamforce'],'The count should be 200');
    }
}

Thanks,
James