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
Jeff Bryant 16Jeff Bryant 16 

Test class for users with LastLoginDate data

I am trying to write a test class that uses the LastLoginDate field but since it is read only I'm not sure as to go about creating data for it. I will share my batch class and what I currently have as a test class...




global class DeactivateUsers implements Database.Batchable<SObject>
{
       dateTime dt = date.today().addDays(-60);
    String pro1 = 'System Administrator';
    public String query = 'SELECT Name, LastLoginDate, Id, user.profile.name From User WHERE IsActive = true AND LastLoginDate <: dt AND user.profile.name <>: pro1 ';

    global Database.querylocator start(Database.BatchableContext bc)
    {
       return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext bc,List<User> records)
    {
        List<User> userList = new List<User>();
        
        for(User r : records)
        {
            User u = (user)r;
            userList.add(u);
        }
        
        if(userList.size() > 0)
        { 
            for(User usr : userList) { usr.isActive = false; }
        }
        update userList;
    }

    global void finish(Database.BatchableContext bc){}
}








@isTest
private class DeactivateUsersTest {
     @testSetup
     static void setup() {
         Profile p = [SELECT Id
                        FROM Profile
                       WHERE Name = 'Standard User'];

         List<User> users = new List<User>();

         for(Integer i = 0; i < 10; i++){
             users.add(new User(Alias = 'Elvis' + i,
                                Email = 'Elvis' + i + '@testorg.com ',
                                EmailEncodingKey = 'UTF-8',
                                LastName = 'Presley',
                                LanguageLocaleKey = 'en_US',
                                LocaleSidKey = 'en_US',
                                ProfileId = p.Id,
                                TimeZoneSidKey = 'America/Los_Angeles',
                                UserName = 'ElvisPresley000' + i + '@testorg.com'));
         }
         insert users;
     }

     static testmethod void testBatch(){
         Test.startTest();
         DeactivateUsers d = new DeactivateUsers();
         Database.executeBatch(d);
         Test.stopTest();
     }
    
    static testmethod void testScheduler(){
        String cronExpr = '0 0 0 15 3 ? 2022';
        
        DeactivateUsersScheduler das = new DeactivateUsersScheduler();
        String jobId = System.schedule('jobTestName', cronExpr, das);
        das.execute(null);
    }
}
Best Answer chosen by Jeff Bryant 16
RituSharmaRituSharma
Create a CSV file to load the data for the test class along with  Last Login Date and run the test class .the Test.loadData method. 
 
List<sObject> ls = Test.loadData(User.sObjectType, 'myResource');

Refer this link for details -> 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_load_data.htm

All Answers

RituSharmaRituSharma
Create a CSV file to load the data for the test class along with  Last Login Date and run the test class .the Test.loadData method. 
 
List<sObject> ls = Test.loadData(User.sObjectType, 'myResource');

Refer this link for details -> 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_load_data.htm
This was selected as the best answer
Priyesh Misquith 12Priyesh Misquith 12
@rituSharma / @Jeff Bryant 16

Could you tell me how did you create the users from the static.
when i try i am getting following error.
'Validation Errors While Saving Record(s)'
sample csv data or code might help.