+ Start a Discussion
mark TMmark TM 

Test class for my batch class

Hi ,

 

Please help me to write a test class for the below batch class written,

 

global class logdeletion implements Database.Batchable<sObject>{
    public String query{get;set;}
    public String email{get;set;}
    //public Id toUserId{get;set;}
   // public Id fromUserId{get;set;}
    
    global Database.querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('Select Comments__c,Name,Owner,CreatedBy,LastModifiedBy,Date_TimeStamp__c,isSuccess__c,Report_URL__c,Result_String__c,Sample_Id__c,Sample_Stage__c FROM Log__c' );
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        List<Log__c> Logs = new List<Log__c>();
        
        for(sObject s : scope){
            Log__c a = (Log__c)s;
            
        }        
        delete Logs ;   
        //system.debug('batch apex called'+accns);     
    }
    global void finish(Database.BatchableContext BC){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setToAddresses(new String[] {email});
        mail.setReplyTo('test@test.com');
        //mail.setSenderDisplayName('SFDC Developer Organization');
        //mail.setSubject('Batch Process');
        //mail.setPlainTextBody('Batch Process has completed');
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

 

 


}Thanks

digamber.prasaddigamber.prasad

Hi,

 

Please check below URL and let me know if you have any question on top of it:-

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

 

Happy to help you!

SFDC_LearnerSFDC_Learner

We can also call the individual methods in batch class.

 
Test Class
 
@isTest(seealldata = true)
public class className{
 
public static testmethod void myUnitTest(){
logdeletion objL = new logdeletion ();
Database.batchableContext bc;
// Create list of records in Log__c
objL.start(bc);
objL.execute(bc,lstL);
objL.finish(bc);
}
 
}
mark TMmark TM

Hi,

 

Its not working , its giving constructor not defined, below is my test class

 

@isTest
private class Testlogdeletion  {
    
    static testMethod void TestClass_logdeletion() {
     Test.startTest();
   List<Log__c> st=new List<Log__c>();
   Database.BatchableContext bc;
   logdeletion obj=new logdeletion();
      //st=obj.start(bc);
     Test.stopTest();
    }

}

 

 

tHANKS

 

digamber.prasaddigamber.prasad

Hi just add below code snippet in your batch class and test class should be working fine.

 

public logdeletion(){
}

 Let me know if you still have any issue.

mark TMmark TM

if i add the content below , my test is saving , but my scheduler class is not getting reflected when i try to schedule the class