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
Churchill19Churchill19 

Help with testing my BulkRecordDelete Batch code

Hi all,

can anyone help with a example of test code so i can test the following batch code please?
 
global class BulkRecordDelete implements Database.Batchable<sObject> {

    
    global string obj;
    private string query = 'SELECT Id FROM ';

    global database.querylocator start(Database.BatchableContext BC) {
        query = query + obj;
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope) {
        List<sObject> ob = new List<sObject>();

        for(sObject s : scope) {
            ob.add(s);
        }
        delete ob;
    }
    
    global void finish(Database.BatchableContext BC) {
        
		if (ApplicationClass.BatchEmailsEnabled) {
	        Id userId = UserInfo.getUserId();
	        String notify = [select Email from User where Id = :userId].Email;
	        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	
	        mail.setToAddresses(new String[] {notify});
	        mail.setReplyTo('batch@acme.com');
	        mail.setSenderDisplayName('Batch Processing');
	        mail.setSubject('Batch Process Completed');
	        mail.setPlainTextBody('Bulk ' + obj + ' delete has completed');
	
	        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
		}
    }
}
Thanks...
 
AMIT KAMBOJAMIT KAMBOJ
This should help. Please check this.
http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex_batch_2.htm
 
David ZhuDavid Zhu
This would work for you.
 
@istest
public class BulkRecordDelete_Test
{
    public static testmethod void PositiveTest1()
    {
        ApplicationClass.BatchEmailsEnabled = true;
        
        User u = [select id,name,email from user where isactive = true and profileid in (select id from profile where name = 'System Administrator') limit 1];

        system.runas(u)
        {
    
            list<Account> accs = new list<Account>();
            for (integer i = 0;i<10;i++)
            {
                Account a = new Account(name = 'test' + string.valueof(i));
                accs.add(a);
            }
            insert accs;


        system.assertEquals(10,[select count() from Account]);
        system.assertEquals(0,system.limits.getemailinvocations());

            
            test.starttest();
        
            BulkRecordDelete brd = new BulkRecordDelete();
            brd.obj = 'account';
            
            ID batchprocessid = Database.executeBatch(brd);
            
            test.stoptest();
        

        system.assertEquals(0,[select count() from Account]);
        }
        
        //system.assertEquals(1,system.limits.getemailinvocations());
        
        
        
    }
}



 
Sean Churchill 9Sean Churchill 9
Hi David,

thanks for this it worked :)

Sean.