2

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 = 'test.sindhu@sfdc.com';
    test1.Email = 'sindoorrtest@gmail.com.com';
    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.

  • It really helps if you make the question readable and easy to understand. That implies that you describe the problem you are facing, and what you've already tried yourself to resolve it. May also help if you post the batch code itself. – Samuel De Rycke Dec 17 '13 at 11:23
4

Based on your error message I think the invocation of executeBatch should be done with a higher or the default scope parameter.

ID batchprocessid = Database.executeBatch(captureBatch);

Eventhough you only insert a single user, i suspect this is the problem. Showing your batch code and the query used may provide better answers.

  • +1. The test class should set the query (overwrite the default) to ensure this "freshly inserted test user" gets processed. Real calls to the batch can use the default query hardcoded in the batch class. – eyescream Dec 17 '13 at 12:17
0

Hard to say much without knowing which line fails ;) Is it a compilation error or runtime one? Does it occur in the unit test class or in the batch itself?

captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =:u.Id';

I see few problems here...

  1. Dynamic queries can have bind variables but the variable must be completely resolved (no dots, no function calls are allowed). See also the excellent answer to Elegant way to convert Set<Id> into String for Dynamic SOQL IN comparison
  2. The batch will start in different context. Does the batch run ok when executed outside of unit test? I'm asking because there's a chance by the time you reach that start() the variable binding you've specified will be gone and query will fail. Consider passing Set<Id> to the batch class maybe or create the query as a string without bindings: 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id = \' + u.Id + '\'';

Last but not least, you need to fill in more required fields when you insert an user in unit test... Creating a user with DML should be a nice sample (although I like your syntax with initialization list in new User(...) more, wastes less script statements).

  • Hi,here is the modified test class – Sindoora Dec 17 '13 at 10:34
  • static testMethod void CaptureLastLoginDate() { Schema.User test1 = new Schema.User(); test1.Username = 'test.sindhu@sfdc.com'; test1.Email = 'sindoorrtest@gmail.com.com'; 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'; – Sindoora Dec 17 '13 at 10:37
  • test1.LocaleSidKey = 'en_US'; test1.EmailEncodingKey = 'ISO-8859-1'; – Sindoora Dec 17 '13 at 10:37
  • 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); } } – Sindoora Dec 17 '13 at 10:38
  • 1
    Geez :D Please edit your question to include it there, not was comments :) It's unreadable. – eyescream Dec 17 '13 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.