+ Start a Discussion
sfdc007sfdc007 

Test class help needed for Batch class

Hi,

I have a batch class where i am trying to write the test class for it

Help me how to write the test class for it
 
MY BATCH CLASS :

global class MillisecondBatchConvertController implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        // SOQL Query to fetch the values of hours , minutes and days
        String query = 'SELECT Hours__c, Minutes__c, Days__c FROM PS_TF_Status_Track__c';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, List<PS_TF_Status_Track__c> scope)
    {
         for(PS_TF_Status_Track__c currPS: scope)
         {
            long hoursToMilliSeconds  = Integer.Valueof(currPS.Hours__c != null ? currPS.Hours__c * 3600000 : 0);
            long minToMilliSeconds = Integer.Valueof(currPS.Minutes__c != null ? currPS.Minutes__c * 60000 : 0);
            long daysToMilliSeconds =  Integer.Valueof(currPS.Days__c != null ? currPS.Days__c * 86400000 : 0);
        
             // Enter your custom field to sum all three fields values.
             currPS.Total_Milliseconds__c =  hoursToMilliSeconds + minToMilliSeconds + daysToMilliSeconds;

         }
         update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}
 Kindly help me pls

Thanks in Advance
 
Best Answer chosen by sfdc007
ManojjenaManojjena
HI Sfdc007,

Try with below code and let me know if it helps
 
@isTest
Public class TestMillisecondBatchConvertController{
  public static testMethod void testBatch() {
	List <PS_TF_Status_Track__c> stList = new List<PS_TF_Status_Track__c>();
	for(integer count = 0; count<200; count++){
		PS_TF_Status_Track__c st = new PS_TF_Status_Track__c(Hours__c=count,Minutes__c=count,Days__c=count); 
		stList.add(st);
	}

	insert stList;
	
	Test.startTest();
	   MillisecondBatchConvertController mbcCon=new MillisecondBatchConvertController();
		ID batchprocessid = Database.executeBatch(mbcCon);
	Test.stopTest();
   }
}
Thanks
Mnaoj


 

All Answers

ManojjenaManojjena
HI Sfdc007,

Try with below code and let me know if it helps
 
@isTest
Public class TestMillisecondBatchConvertController{
  public static testMethod void testBatch() {
	List <PS_TF_Status_Track__c> stList = new List<PS_TF_Status_Track__c>();
	for(integer count = 0; count<200; count++){
		PS_TF_Status_Track__c st = new PS_TF_Status_Track__c(Hours__c=count,Minutes__c=count,Days__c=count); 
		stList.add(st);
	}

	insert stList;
	
	Test.startTest();
	   MillisecondBatchConvertController mbcCon=new MillisecondBatchConvertController();
		ID batchprocessid = Database.executeBatch(mbcCon);
	Test.stopTest();
   }
}
Thanks
Mnaoj


 
This was selected as the best answer
David ZhuDavid Zhu
You may refer the code from 

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_batch_1.htm
https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_batch_2.htm


The first part is building the testing data. In your case, you needs to build some record to PS_TF_Status_Track__c object.
// Create some test items to be update by the batch job. 
PS_TF_Status_Track__c [] ml = new List<PS_TF_Status_Track__c >();
for (Integer i=0;i<10;i++) { 
     PS_TF_Status_Track__c m = new PS_TF_Status_Track__c 
                                       ( Name='item ' + i, 
                                         hours__c= 3,
                                         Minutes__c= 20, 
                                         Days__c= 1); 
                                         ml.add(m); 
              } 
insert ml;


The second part is to run the batch class. Use
Test.startTest(); 
MillisecondBatchConvertController c = new MillisecondBatchConvertController(); 
Database.executeBatch(c); 
Test.stopTest();


The last part is to verify the result.