I'm trying to search in enternet but no results….
So i haveing a batch that updates records (IT_Asset__c) if the FrstDayOfMnth__c = true. After that fires the trigger.
The apex test class not covering the execute method without SeeAllData=true.
here my execute method:
//Execute method
global void execute (Database.BatchableContext BC, List<sobject> scope) {
List<IT_Asset__c> ITAList = new List<IT_Asset__c>();
List<IT_Asset__c> updtaedITAList = new List<IT_Asset__c>();
for (sObject objScope: scope) {
IT_Asset__c newObjScope = (IT_Asset__c)objScope ;
newObjScope.FrstDayOfMnth__c = true;
updtaedITAList.add(newObjScope);
System.debug('Value of UpdatedITAList'+updtaedITAList);
}
if (updtaedITAList != null && updtaedITAList.size()>0) {
Database.update(updtaedITAList); System.debug('List Size '+updtaedITAList.size());
}
}
And here my test class:
@isTest(SeeAllData=false)
global class TestITAssetProcessingBatchClass {
public static testmethod void TestBatch(){
ITAssetProcessingBatch c = new ITAssetProcessingBatch();
c.query = 'Select id From IT_Asset__c Limit 200';
Account acc = new Account();
acc.Name = 'Test Account';
insert acc;
List<IT_Asset__c> itass = new List<IT_Asset__c>();
for(integer i = 0; i < 25; i++){
IT_Asset__c it = new IT_Asset__c(Account__c = acc.Id, FrstDayOfMnth__c = true, Quantity__c = 4);
itass.add(it);
}
insert itass;
Test.startTest();
Database.executeBatch(c, 200);
Test.stopTest();
Integer listCont = [select COUNT() from IT_Asset__c where Account__c = :acc.id ];
System.assertEquals(listCont, listCont);
}
}
Please point me, what i'm doing wrong?
Best Answer
When setting up batch executions, I find it is often very helpful to add a number of
System.assert()
statements that verify that the data setup was correct before moving on to behavior assertions. I see after yourTest.stopTest()
you are doing a record count check, but that check is (a) occurs after your behavior testing, and (b) uses a different query than your actual batch job.For this test, you can improve that setup check by changing the following:
to...
I am not sure what the actual problem for you is, but adding this style of assert will not only help you troubleshoot now, but will help ensure that future changes to your org do not break the preconditions for this test.