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
Jyoti TrailheadJyoti Trailhead 

Need help in Test Class

Hi 

I am new to Apex and created a class to delete Assets from Base Element class of particular status - here is the code : please help me in creating test class for code coverage.

##############################################################

global class BMCRF_ScheduledDeleteAssets Implements Schedulable {
  global void execute(SchedulableContext sc) {
       massDelete();
    }    
          // Query the accounts to delete
        public void massDelete() {
        List <BMCServiceDesk__BMC_BaseElement__c> listtoDelete = [Select Id FROM BMCServiceDesk__BMC_BaseElement__c WHERE BMCServiceDesk__CI_Status__c =: 'Disposal'];

        // Delete the assets
        Database.DeleteResult[] drList = Database.delete(listtoDelete, false);
           // Iterate through each returned result
               for(Database.DeleteResult dr : drList) {
               if (dr.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully deleted account with ID: ' + dr.getId());
                }
                else {
                    for(Database.Error err : dr.getErrors()) {
                    System.debug('The following error has occurred.');                   
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Account fields that affected this error: ' + err.getFields());
                     }
                    }
                }
     #########################################################

      
            }
            }
Best Answer chosen by Jyoti Trailhead
Amit Chaudhary 8Amit Chaudhary 8
PLease try below code.
@isTest
private class BMCRF_ScheduledDeleteAssetsTest 
{

   public static String CRON_EXP = '0 0 0 15 3 ? 2022';

   static testmethod void test() 
   {
		//BMCServiceDesk__BMC_BaseElement__c obj = new BMCServiceDesk__BMC_BaseElement__c();
		//obj.name ='demo';
		// add all required field
		/insert obj;
   
      Test.startTest();

      String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new BMCRF_ScheduledDeleteAssets() );
         
      CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered,   NextFireTime  FROM CronTrigger WHERE id = :jobId];

      System.assertEquals(CRON_EXP,  ct.CronExpression);

   }
}
Let us know if this will help u

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
PLease try below code.
@isTest
private class BMCRF_ScheduledDeleteAssetsTest 
{

   public static String CRON_EXP = '0 0 0 15 3 ? 2022';

   static testmethod void test() 
   {
		//BMCServiceDesk__BMC_BaseElement__c obj = new BMCServiceDesk__BMC_BaseElement__c();
		//obj.name ='demo';
		// add all required field
		/insert obj;
   
      Test.startTest();

      String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new BMCRF_ScheduledDeleteAssets() );
         
      CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered,   NextFireTime  FROM CronTrigger WHERE id = :jobId];

      System.assertEquals(CRON_EXP,  ct.CronExpression);

   }
}
Let us know if this will help u

 
This was selected as the best answer
Jyoti TrailheadJyoti Trailhead
Thanks a lot Amit - it worked and has 75% code coverage, can you please help me in understanding the logic briefly.

Regards
Jyoti
 
Amit Chaudhary 8Amit Chaudhary 8