+ Start a Discussion
Kevin Tullos 7Kevin Tullos 7 

Test Code for Delete Batch

Please help me with this one...

I wrote a batch the will delete all records in a called sobject.  Here is the code...
 
global class Delete_Batch implements Database.Batchable<sObject>
{

    String query, value, field;
    
    global Delete_Batch(string s)
    {
        Query=s;
        //query = 'SELECT Id FROM Delete_Batch_Test__c';
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
       return Database.getQueryLocator(query); 
    }
   
    global void execute(Database.BatchableContext BC, List<sObject> scope)
    {
        Delete Scope;
    }
     
    global void finish(Database.BatchableContext BC)
    {
    }
}

My Test code isn't working though because I have problem with declaring the sobject as a variable.  Please help...
 
@IsTest
public class testDelete_Batch {

    static testmethod void insertTask()  {
    
    Task t = new Task();
    Task t2 = new Task();
    
    t.status='Not Started';
    t.Priority = 'High';
    t.Description = 'Description';
    t.activitydate = system.today();
    
    insert t;
    
    t2.status='Not Started';
    t2.Priority = 'High';
    t2.Description = 'Description';
    t2.activitydate = system.today()-15;
    
    insert t2;
    
    t.Description= 'Visit';
Update t;
//Id batchInstanceID = Database.executeBatch(new Delete_Batch());
Delete_Batch_Method(task,1);
Delete t;
    }}

Thanks.

Kevin
Best Answer chosen by Kevin Tullos 7
Pradeep Kumar L.GPradeep Kumar L.G
Try below code: 
String query = 'SELECT Id FROM Delete_Batch_Test__c';
       Test.startTest();
       Delete_Batch c = new Delete_Batch(query);
       Database.executeBatch(c);
       Test.stopTest();
For more info : http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex_batch_2.htm.

Thanks
Pradeep

Mark this answer solved if this resolve your problem.