You need to sign in to do that
Don't have an account?
Jeff 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);
}
}
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);
}
}
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
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
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.