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<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);
}
}
ERROR:System.UnexpectedException: No more than one executeBatch can be called from within a testmethod. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.