+ Start a Discussion
Ross Gilbert 31Ross Gilbert 31 

Error on batch job test class

This batch job works but the test class I'm writing for it throws this 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.

Batch job that works:
global class UpdateUser implements Database.Batchable<sObject> {

    string query;
    global void execute(SchedulableContext SC) 
        UpdateUser x = new UpdateUser(); 

    global Database.querylocator start(Database.BatchableContext BC){
        Query = 'Select id, Contact.Last_Login_to_Portal_Community__c, LastLoginDate FROM User WHERE ContactId != null AND isActive = true';
        return Database.getQueryLocator(query);

    global void execute(Database.BatchableContext BC, List<User> scope1){
        List<Contact> cList = new List<Contact>();
        List<User> uList = new List<User>();
            for(User u : Scope1){
                u.Last_Login_Date__c = u.LastLoginDate; 
                u.Contact.Last_Login_to_Portal_Community__c = u.LastLoginDate;
        update uList;
        update cList;

    global void finish(Database.BatchableContext BC){
    Schedule job every 30 minutes:
    updateUser us = new updateUser ();
    string sch = '0 30 * * 1-12 ? *';

Test class for the above job that doesn't work:
private class TestCleanUpBatchClass {

    static testmethod void test() {
        //insert an account
        Account a = new Account();
        a.Name = 'test';
        insert a;
        //insert a contact
        Contact c = new Contact();
        c.LastName ='test';
        c.FirstName ='test';
        insert c;
        //insert a user on that contact
        Profile p = [SELECT Id FROM Profile WHERE Name='Sovos Customer Community Login User - Convey'];        
        User u = new User(Alias = 'standt1', Email='standarduser1@testorg1.com', 
          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
          LocaleSidKey='en_US', ProfileId = p.Id, ContactId = c.Id, Last_Login_Date__c = System.today(),
          TimeZoneSidKey='America/Los_Angeles', UserName='standarduser1@testorg1.com');    
          insert u;    
        //run the batch
          UpdateUser x = new UpdateUser();
          ID batchprocessid = Database.executeBatch(x);
        //confirm that the contact Last Login to Portal/Community is = to user last login
        Contact c1 = [SELECT Id, Last_Login_to_Portal_Community__c FROM Contact Where Id =: c.Id];
        User u1 = [SELECT Id, ContactId, Last_Login_Date__c FROM User Where Id =: u.Id];

Anyone know what I'm doing wrong here to get that error?  Thanks!