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
James George 717James George 717 

Batch Apex and System.Assert clarification, please help

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
Suraj Tripathi 47Suraj Tripathi 47

Hi James,

If you are querying the data in test class then you have to use Test

@isTest
private class LeadProcessorTest{
 
    @testSetup 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;       
    }
    
    @isTest static void testMethod1() {
	        List<Lead> l_lstX = [SELECT Id, LastName, LeadSource FROM Lead];
            System.assertEquals(200 ,l_lstX.size());

	    Test.startTest();
        LeadProcessor lp = new LeadProcessor();
        Database.executeBatch(lp);
        Test.stopTest();
        
	}	
		
    }

Please mark it as the Best Answer so that other people would take reference from it.

Thank You

 

mukesh guptamukesh gupta
Hi Jeams,

Please use below code:-
 
@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);
        }

integer  count = [select count() from Lead where LeadSource = 'Dreamforce'];
        System.assertEquals(200, count );
    }
}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
James George 717James George 717
Hi Mukesh,
Thanks for your comments and code, I tried your code but still the assertion fails.
Please see the screenshot.
User-added image
Again when I just do the assert for the count, it gives success
integer  count = [select count() from Lead];

but when tried with below code, assertion fails
integer  count = [select count() from Lead where LeadSource = 'Dreamforce'];

I need to know why the values set by the batch class is not reflected in the Test class.
Is this the way to test the batch class to get only the count without the WHERE Clause.

Thanks again,
James
James George 717James George 717
Hi Suraj,
Thanks for your reply,
I'm not looking for the count of how many records inserts and asserts based on it, but the value set by the batch class.
which is l.LeadSource = 'Dreamforce';

integer  count = [select count() from Lead where LeadSource = 'Dreamforce'];
why this is failing? any ideas

If the values set by the batch process cannot be handled in the test class how can we know for sure the field got updated with the correct values, in my case Dreamforce?

Thanks,
James
Anita GargAnita Garg
Hi James,

I am facing the same issue. How did you resolve this?

Thanks,
Anita
Jithin ChandJithin Chand
Hello All, 

I know its too late to answer but for others who might get the issues in the future.

The batch updates will not be available for assertion inside the test context.(ie; before test.stoptest).

To assert for batch update, after the test.stoptest, query the records and then do the assertion. This should help.