+ Start a Discussion
Roy SinghRoy Singh 

Could anyone help me write test class for below class

Hi 

public class DeactivateInactiveUsers implements Schedulable {
    public void execute(SchedulableContext context) {
        User[] selectedUsers = [SELECT Id FROM User WHERE IsActive = TRUE AND Id NOT IN (SELECT UserId FROM LoginHistory WHERE LoginTime = LAST_N_DAYS:8)];
        for(User record: selectedUsers) {
            record.IsActive = false;
        }
        Database.update(selectedUsers, false);
    }
}

Thanks
Best Answer chosen by Roy Singh
Ashif KhanAshif Khan
Hi Roy
@isTest
public class DeactivateInactiveUsersTest {
       public static testMethod void testschedule() {
        Test.StartTest();
        DeactivateInactiveUsers sh1 = new DeactivateInactiveUsers();
        String sch = '0 0 23 * * ?'; 
        system.schedule('Test Territory Check', sch, sh1); 
        Test.stopTest(); 
    }
}

I hope this will help you if it works mark question as solved

Regards
Ashif 

All Answers

Ashif KhanAshif Khan
Hi Roy
@isTest
public class DeactivateInactiveUsersTest {
       public static testMethod void testschedule() {
        Test.StartTest();
        DeactivateInactiveUsers sh1 = new DeactivateInactiveUsers();
        String sch = '0 0 23 * * ?'; 
        system.schedule('Test Territory Check', sch, sh1); 
        Test.stopTest(); 
    }
}

I hope this will help you if it works mark question as solved

Regards
Ashif 
This was selected as the best answer
Roy SinghRoy Singh
Thanks Ashif 
could you please tell me 
String sch='0 0 23 * * ?';      how to use this ?
Ashif KhanAshif Khan
Hi Roy,
we set execution time of scheduler with the help of this string.
as given below.

   *              *             *              *                 *             *                    *
Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year   

to know in detail 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

here in apex guide explained very well.

Thanks
Ashif