You need to sign in to do that
Don't have an account?
James 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
LeadProcessorTest.class
Thanks,
James
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
Hi James,
If you are querying the data in test class then you have to use Test
Please mark it as the Best Answer so that other people would take reference from it.
Thank You
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Thanks for your comments and code, I tried your code but still the assertion fails.
Please see the screenshot.
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
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
I am facing the same issue. How did you resolve this?
Thanks,
Anita
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.