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
Linda 98Linda 98 

Lastmodifieddate in test class for batch schedule class

I am changing one particular  field by checking lastmodifeddate in my code.All works fine but i am struck with test class.I am chnaging multiple records from multiple objects so my batch class looks like below.
 
global class editrecords implements Database.Batchable<string>, Schedulable{
global boolean bool = false;

global Iterable<string> start(Database.BatchableContext ctx) {
    return new list<String> { 'CO__c', 'CO2__c', 'CO3__c','CO4__c'};
}
global void execute(Database.BatchableContext ctx, list<string> listObj) {
    list<sObject> editrecords = new list<sObject>();
    for(string strObjName : listObj) {
        for(sObject objsObject : database.query('Select Id from ' + strObjName + ' where LastModifiedDate < LAST_N_DAYS:36')) {
                editrecords.add(objsObject);
            else {
                b = true;
                break;
            }
        }
    }
    update editrecords;
}
global void finish(Database.BatchableContext ctx) {

}        global void execute(SchedulableContext sc){
         Database.executebatch(new editrecords ());    
    }
}

my test class is as below which get 58% of coverage.How can i get lastmodified date and cover rest of code.please help.
 
@isTest
Public class testeditrecords{

    static testMethod void testMethod1(){

        CO__c CO =new CO__c(Name='testdata');
        insert CO;
        CO2__c co2 =new 
  co2__c(Name='testdata');
        insert co2;
        Co3__c co3 =new Co3__c(Name='test2');
        insert co3;

       for(CO__c c:[Select ID from CO__c where createddate=TODAY]){
        Test.startTest();
       
        editrecords obj = new editrecords();
        Database.executeBatch(obj);
        Test.stopTest();       
        }
   } 
}

 
Best Answer chosen by Linda 98
Amit Chaudhary 8Amit Chaudhary 8
Try to update your test class like below
@isTest
Public class testeditrecords{

    static testMethod void testMethod1(){

        CO__c CO =new CO__c(Name='testdata');
        insert CO;
        
		CO2__c co2 =new co2__c(Name='testdata');
        insert co2;
		
        Co3__c co3 =new Co3__c(Name='test2');
        insert co3;

			Test.startTest();
				bool 
				String CRON_EXP = '0 0 0 15 3 ? *';
				editrecords obj = new editrecords();
				obj.bool = false;
				String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, obj );
				CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
				System.assertEquals(CRON_EXP, ct.CronExpression);
				System.assertEquals(0, ct.TimesTriggered);
			Test.stopTest();       
	} 
}

Let us know if this will help you
 

All Answers

bhanu_prakashbhanu_prakash
Hi Linda,
Mark as best answer, If it resloves !!
CO__c CO =new CO__c(Name='testdata'); 
CO.cDate = c.CreatedDate;
insert CO;
Create date field in test class for these object CO__c  and update it.
So you will get created date as last modified date.

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com (https://www.forcelearn.com/)
Amit Chaudhary 8Amit Chaudhary 8
Try to update your test class like below
@isTest
Public class testeditrecords{

    static testMethod void testMethod1(){

        CO__c CO =new CO__c(Name='testdata');
        insert CO;
        
		CO2__c co2 =new co2__c(Name='testdata');
        insert co2;
		
        Co3__c co3 =new Co3__c(Name='test2');
        insert co3;

			Test.startTest();
				bool 
				String CRON_EXP = '0 0 0 15 3 ? *';
				editrecords obj = new editrecords();
				obj.bool = false;
				String jobId = System.schedule('ScheduleApexClassTest',  CRON_EXP, obj );
				CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
				System.assertEquals(CRON_EXP, ct.CronExpression);
				System.assertEquals(0, ct.TimesTriggered);
			Test.stopTest();       
	} 
}

Let us know if this will help you
 
This was selected as the best answer
Linda 98Linda 98
Thank you Amit.Now i am having 76% of code coverage.Part which is not being covered is 
editrecords.add(objsObject);
            else {
                b = true;
                break;