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
Ramana VRamana V 

Test class batch class update

Hi All,
I am facing issues with test class for batch apex. Please find below my classes.

Batch Apex:
 
global class conuserDeactivate implements Database.Batchable<sObject>{
   
    global Database.Querylocator Start(Database.BatchableContext BC)
    {
		set<id> userId = new set<id>();
        List<contact> conList = [select id,User__c from contact where user__r.lastlogindate < =:syste.now().adddays(-14)];        
       for(contact c : caseList){
            uid.add(c.User__c);
           
        }
        string s = 'select isActive from user where id =: userid';    
        return Database.getQuerylocator(s);
    }
   
    global void Execute(Database.BatchableContext BC, List<User> scope){      
        for(User u : scope){
            u.isActive = false;
        }        
        update scope;
    }  
   
    global void finish(Database.BatchableContext BC){
       
    }
}

Test class:
@isTest
public class conuserDeactivateTest {
    static testMethod void deactivateMethod() {
        
        List<sObject> us = Test.loadData(User.sObjectType, 'userResource');
        User u = (User)us[0];
        Id uid = u.id;    
        
        Contact c = new Contact();
        c.LastName = 'TestContact';
       
        c.UserB__c = uid;
        insert c;
       
        Test.startTest();
        conuserDeactivate CUD = new conuserDeactivate();
        DataBase.executeBatch(CUD);            
        Test.stopTest();
    }
}


​​​​​​​
I am facing issues in covering of for loop in start method. I am not able to get user based on last log in date in test class.
Please help me how to cover this in test class.
Thanks in Advance
Regards,
Ramana V
 
Ryan Khan 1Ryan Khan 1
Any web developer to help me in; bestelectricsmoker.org (https://bestelectricsmoker.org/)