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
AkiTAkiT 

Batch apex tests failing

 Can anyone see what's wrong here ? Still not able to get the code assertions to succeed. :smileyindifferent::smileysad::smileymad:
 
The simple batch process reparents account's contacts. It works ok, but in the testing I am not able to get assertions to succeed. 
 

global class BatchMerger implements Database.Batchable<SObject>{

  private final String query; global BatchMerger(String q){query = q;} global database.querylocator start(Database.BatchableContext BC){return Database.getQueryLocator(query);} global void execute(Database.BatchableContext BC, Sobject[] scope){ Map <Id, Account> dupes = new Map <Id, Account>(); for(sobject s : scope){ Account a = (Account)s; dupes.put(a.Id, a); } // Look for dupes Contacts List <Contact> contacts = new List <Contact>(); for(Contact c : [select Id, AccountId from Contact where AccountId in :dupes.keySet()]){ c.AccountId = dupes.get(c.AccountId).Real_Acct__c; contacts.add(c); } // updates Database.Saveresult[] src=Database.update(contacts); } global void finish(Database.BatchableContext BC){ }}

 

TEST DATA CREATIONS...

 

...

String query = 'select Id, Real_Acct__c, dupe__c ' +'from Account ' +'where Real_Acct__c != null ' +'and dupe__c = true ' +'limit 200'; BatchMerger cln = new BatchMerger(query); Id batchProcessId = Database.executeBatch(cln); Test.StopTest(); AsyncApexJob[] jobs = [select TotalJobItems, Status, Id from AsyncApexJob where Id = :batchProcessId]; system.assertEquals(1, jobs[0].TotalJobItems); <-- THIS IS OK! system.assertEquals(0, [select count() from Opportunity where AccountId = :dupes[0].Id]); <-- THIS FAILS, actual being 1 - the one opp in that in test data was assigned to this account dupes[0].

 

Thanks for help!

  
Best Answer chosen by Admin (Salesforce Developers) 
AkiTAkiT

I cut my code a bit to make it easier to read so I cut mistakenly the Opportunity part away. Anyway both con & opp were there in the code...

 

But maybe exhausted of this new feature I fooled myself by forgetting the data that existed in my test environment... so although I was setting my test environment by creating my data I forgot that batch size in test was that 200. So it never processed my actual test data which is why my assertions failed!!

 

Anyway now all working great and life's good  :smileywink:

All Answers

ThomasTTThomasTT

It's not about coding, but your application's logic. It is very difficult for other people to understand why you coded the testmethod.

 

For example, I don't get why the count() of Opportunity is expected as 0... You re-set Contact.AccountId in the batch. Where is Opportunity, anyway?

 

I guess that's the reason why nobody could answer your original post... or I'm just too stupid.

 

ThomasTT

AkiTAkiT

I cut my code a bit to make it easier to read so I cut mistakenly the Opportunity part away. Anyway both con & opp were there in the code...

 

But maybe exhausted of this new feature I fooled myself by forgetting the data that existed in my test environment... so although I was setting my test environment by creating my data I forgot that batch size in test was that 200. So it never processed my actual test data which is why my assertions failed!!

 

Anyway now all working great and life's good  :smileywink:

This was selected as the best answer