Skip to main content
added unit-test tag
Link
Peter Knolle
  • 29.2k
  • 12
  • 104
  • 164
added 266 characters in body
Source Link
Samuel De Rycke
  • 9.6k
  • 8
  • 46
  • 73

global class CaptureLastLoginDate implements Database.Batchable{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId(); String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) { System.debug(query); return database.getQueryLocator(query); } //close start method global void execute(Database.BatchableContext BC, List scope){ List<Schema.User> u = new List<Schema.User> (); for(sObject s: Scope){ Schema.User inv = (Schema.User)s; inv.LastLoginDate__c= inv.LastLoginDate; u.add(inv); System.debug(u); } update u;

}

global class CaptureLastLoginDate implements Database.Batchable<sObject>{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId();
String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) {
System.debug(query);
return database.getQueryLocator(query);
}
//close start method
global void execute(Database.BatchableContext BC, List<sObject> scope){
    List<Schema.User> u = new List<Schema.User> ();
    for(sObject s: Scope){
        Schema.User inv = (Schema.User)s;
        inv.LastLoginDate__c= inv.LastLoginDate;
        u.add(inv);
        System.debug(u);
    }
    update u;

}

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

}

TEST CLASS:

@IsTest Private class LastLogintest { //Datetime LastLoginDate__c; static testMethod void CaptureLastLoginDate() { Schema.User test1 = new Schema.User(); test1.Username = '[email protected]'; test1.Email = '[email protected]'; test1.Lastname = 'Gop'; test1.Firstname = 'Sindoo'; test1.Alias = 'sgopa'; test1.CommunityNickname = '1234'; test1.UserRole = [select id from UserRole where id=:'00ED0000001BsGj'];======================================================================== SObject prof = [ select id from profile where id =TEST CLASS:'00eD0000001o9A0' ]; test1.ProfileId = (ID) prof.get('ID');

@IsTest
     Private class LastLogintest
     {
     //Datetime LastLoginDate__c;
     static testMethod void CaptureLastLoginDate()
     {
    Schema.User test1 = new Schema.User();
    test1.Username = '[email protected]';
    test1.Email = '[email protected]';
    test1.Lastname = 'Gop';
    test1.Firstname = 'Sindoo';
    test1.Alias = 'sgopa';
    test1.CommunityNickname = '1234';
    test1.UserRole = [select id from UserRole where id=:'00ED0000001BsGj'];
    SObject prof = [ select id from profile where id =:'00eD0000001o9A0' ];
    test1.ProfileId = (ID) prof.get('ID');
    
    test1.CurrencyIsoCode = 'EUR';
    test1.TimeZoneSidKey = 'GMT';
    test1.LocaleSidKey = 'en_US';
    test1.EmailEncodingKey = 'ISO-8859-1';
    test1.LanguageLocaleKey = 'en_US';
    test1.UserPermissionsMobileUser = false;
      test1.LastLoginDate__c=null;
    insert test1;
    System.debug ( JSON.serializePretty(test1 ) );
    Test.startTest();
        CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
        // Modify the query here so we only get our one testing user of interest
        //captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =:test1.Id';
        ID batchprocessid = Database.executeBatch(captureBatch,1);
        Test.StopTest();
    
        Schema.User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:test1.Id limit 1];
        System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
    }
     
    }

global class CaptureLastLoginDate implements Database.Batchable{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId(); String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) { System.debug(query); return database.getQueryLocator(query); } //close start method global void execute(Database.BatchableContext BC, List scope){ List<Schema.User> u = new List<Schema.User> (); for(sObject s: Scope){ Schema.User inv = (Schema.User)s; inv.LastLoginDate__c= inv.LastLoginDate; u.add(inv); System.debug(u); } update u;

}

global void finish(Database.BatchableContext sc){
}

}

TEST CLASS:

@IsTest Private class LastLogintest { //Datetime LastLoginDate__c; static testMethod void CaptureLastLoginDate() { Schema.User test1 = new Schema.User(); test1.Username = '[email protected]'; test1.Email = '[email protected]'; test1.Lastname = 'Gop'; test1.Firstname = 'Sindoo'; test1.Alias = 'sgopa'; test1.CommunityNickname = '1234'; test1.UserRole = [select id from UserRole where id=:'00ED0000001BsGj']; SObject prof = [ select id from profile where id =:'00eD0000001o9A0' ]; test1.ProfileId = (ID) prof.get('ID');

test1.CurrencyIsoCode = 'EUR';
test1.TimeZoneSidKey = 'GMT';
test1.LocaleSidKey = 'en_US';
test1.EmailEncodingKey = 'ISO-8859-1';
test1.LanguageLocaleKey = 'en_US';
test1.UserPermissionsMobileUser = false;
  test1.LastLoginDate__c=null;
insert test1;
System.debug ( JSON.serializePretty(test1 ) );
Test.startTest();
    CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
    // Modify the query here so we only get our one testing user of interest
    //captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =:test1.Id';
    ID batchprocessid = Database.executeBatch(captureBatch,1);
    Test.StopTest();

    Schema.User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:test1.Id limit 1];
    System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
}
 
}
global class CaptureLastLoginDate implements Database.Batchable<sObject>{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId();
String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) {
System.debug(query);
return database.getQueryLocator(query);
}
//close start method
global void execute(Database.BatchableContext BC, List<sObject> scope){
    List<Schema.User> u = new List<Schema.User> ();
    for(sObject s: Scope){
        Schema.User inv = (Schema.User)s;
        inv.LastLoginDate__c= inv.LastLoginDate;
        u.add(inv);
        System.debug(u);
    }
    update u;

}

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

======================================================================== TEST CLASS:

@IsTest
     Private class LastLogintest
     {
     //Datetime LastLoginDate__c;
     static testMethod void CaptureLastLoginDate()
     {
    Schema.User test1 = new Schema.User();
    test1.Username = '[email protected]';
    test1.Email = '[email protected]';
    test1.Lastname = 'Gop';
    test1.Firstname = 'Sindoo';
    test1.Alias = 'sgopa';
    test1.CommunityNickname = '1234';
    test1.UserRole = [select id from UserRole where id=:'00ED0000001BsGj'];
    SObject prof = [ select id from profile where id =:'00eD0000001o9A0' ];
    test1.ProfileId = (ID) prof.get('ID');
    
    test1.CurrencyIsoCode = 'EUR';
    test1.TimeZoneSidKey = 'GMT';
    test1.LocaleSidKey = 'en_US';
    test1.EmailEncodingKey = 'ISO-8859-1';
    test1.LanguageLocaleKey = 'en_US';
    test1.UserPermissionsMobileUser = false;
      test1.LastLoginDate__c=null;
    insert test1;
    System.debug ( JSON.serializePretty(test1 ) );
    Test.startTest();
        CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
        // Modify the query here so we only get our one testing user of interest
        //captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =:test1.Id';
        ID batchprocessid = Database.executeBatch(captureBatch,1);
        Test.StopTest();
    
        Schema.User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:test1.Id limit 1];
        System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
    }
     
    }
added 1092 characters in body
Source Link
Sindoora
  • 735
  • 1
  • 11
  • 24

I wrote a batch apex,test class on user.and when running the test class it is failed and below is the batch class and test class on which i'm working

Batch Class

global class CaptureLastLoginDate implements Database.Batchable{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId(); String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) { System.debug(query); return database.getQueryLocator(query); } //close start method global void execute(Database.BatchableContext BC, List scope){ List<Schema.User> u = new List<Schema.User> (); for(sObject s: Scope){ Schema.User inv = (Schema.User)s; inv.LastLoginDate__c= inv.LastLoginDate; u.add(inv); System.debug(u); } update u;

}

@IsTest
 Private class LastLogintest
 {
 //Datetime LastLoginDate__c;
 static testMethodglobal void CaptureLastLoginDate()
 {
Schema.User test1 = new Schema.Userfinish();
test1.Username = '[email protected]';
test1.Email = '[email protected]';
test1.Lastname = 'Gop';
test1.Firstname = 'Sindoo';
test1.Alias = 'sgopa';
test1.CommunityNickname = '1234';
test1.UserRole = [select id from UserRole where id=:'00ED0000001BsGj'];
SObject prof = [ select id from profile where id =:'00eD0000001o9A0' ];
test1Database.ProfileId = (ID)BatchableContext prof.get('ID'sc);{
}

}

TEST CLASS:

@IsTest Private class LastLogintest { //Datetime LastLoginDate__c; static testMethod void CaptureLastLoginDate() { Schema.User test1 = new Schema.User(); test1.Username = '[email protected]'; test1.Email = '[email protected]'; test1.Lastname = 'Gop'; test1.Firstname = 'Sindoo'; test1.Alias = 'sgopa'; test1.CommunityNickname = '1234'; test1.UserRole = [select id from UserRole where id=:'00ED0000001BsGj']; SObject prof = [ select id from profile where id =:'00eD0000001o9A0' ]; test1.ProfileId = (ID) prof.get('ID');

test1.CurrencyIsoCode = 'EUR';
test1.TimeZoneSidKey = 'GMT';
test1.LocaleSidKey = 'en_US';
test1.EmailEncodingKey = 'ISO-8859-1';
test1.LanguageLocaleKey = 'en_US';
test1.UserPermissionsMobileUser = false;
  test1.LastLoginDate__c=null;
insert test1;
System.debug ( JSON.serializePretty(test1 ) );
Test.startTest();
    CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
    // Modify the query here so we only get our one testing user of interest
    //captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =:test1.Id';
    ID batchprocessid = Database.executeBatch(captureBatch,1);
    Test.StopTest();

    Schema.User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:test1.Id limit 1];
    System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
}
 
}
@IsTest
 Private class LastLogintest
 {
 //Datetime LastLoginDate__c;
 static testMethod void CaptureLastLoginDate()
 {
Schema.User test1 = new Schema.User();
test1.Username = '[email protected]';
test1.Email = '[email protected]';
test1.Lastname = 'Gop';
test1.Firstname = 'Sindoo';
test1.Alias = 'sgopa';
test1.CommunityNickname = '1234';
test1.UserRole = [select id from UserRole where id=:'00ED0000001BsGj'];
SObject prof = [ select id from profile where id =:'00eD0000001o9A0' ];
test1.ProfileId = (ID) prof.get('ID');

test1.CurrencyIsoCode = 'EUR';
test1.TimeZoneSidKey = 'GMT';
test1.LocaleSidKey = 'en_US';
test1.EmailEncodingKey = 'ISO-8859-1';
test1.LanguageLocaleKey = 'en_US';
test1.UserPermissionsMobileUser = false;
  test1.LastLoginDate__c=null;
insert test1;
System.debug ( JSON.serializePretty(test1 ) );
Test.startTest();
    CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
    // Modify the query here so we only get our one testing user of interest
    //captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =:test1.Id';
    ID batchprocessid = Database.executeBatch(captureBatch,1);
    Test.StopTest();

    Schema.User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:test1.Id limit 1];
    System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
}
 
}

I wrote a batch apex,test class on user.and when running the test class it is failed and below is the batch class and test class on which i'm working

Batch Class

global class CaptureLastLoginDate implements Database.Batchable{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId(); String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) { System.debug(query); return database.getQueryLocator(query); } //close start method global void execute(Database.BatchableContext BC, List scope){ List<Schema.User> u = new List<Schema.User> (); for(sObject s: Scope){ Schema.User inv = (Schema.User)s; inv.LastLoginDate__c= inv.LastLoginDate; u.add(inv); System.debug(u); } update u;

}

global void finish(Database.BatchableContext sc){
}

}

TEST CLASS:

@IsTest Private class LastLogintest { //Datetime LastLoginDate__c; static testMethod void CaptureLastLoginDate() { Schema.User test1 = new Schema.User(); test1.Username = '[email protected]'; test1.Email = '[email protected]'; test1.Lastname = 'Gop'; test1.Firstname = 'Sindoo'; test1.Alias = 'sgopa'; test1.CommunityNickname = '1234'; test1.UserRole = [select id from UserRole where id=:'00ED0000001BsGj']; SObject prof = [ select id from profile where id =:'00eD0000001o9A0' ]; test1.ProfileId = (ID) prof.get('ID');

test1.CurrencyIsoCode = 'EUR';
test1.TimeZoneSidKey = 'GMT';
test1.LocaleSidKey = 'en_US';
test1.EmailEncodingKey = 'ISO-8859-1';
test1.LanguageLocaleKey = 'en_US';
test1.UserPermissionsMobileUser = false;
  test1.LastLoginDate__c=null;
insert test1;
System.debug ( JSON.serializePretty(test1 ) );
Test.startTest();
    CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
    // Modify the query here so we only get our one testing user of interest
    //captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =:test1.Id';
    ID batchprocessid = Database.executeBatch(captureBatch,1);
    Test.StopTest();

    Schema.User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:test1.Id limit 1];
    System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
}
 
}
added 163 characters in body
Source Link
Samuel De Rycke
  • 9.6k
  • 8
  • 46
  • 73
Loading
I'm getting error during run time
Source Link
Sindoora
  • 735
  • 1
  • 11
  • 24
Loading
added 22 characters in body
Source Link
Mohith Shrivastava
  • 91.9k
  • 19
  • 161
  • 214
Loading
Source Link
Sindoora
  • 735
  • 1
  • 11
  • 24
Loading