0

I have Schedulable, Batch class

public class Schedule_Batch_CheckUser implements Schedulable ,database.Batchable<sObject>,Database.AllowsCallouts, Database.Stateful{
public void execute(SchedulableContext SC){
    //I'm using this to avoid error "No more than one executeBatch can be called from within a test method."
    if(System.Test.isRunningTest()){
        ID jobID = Database.executeBatch(new Schedule_Batch_CheckUser(), 50);
        System.abortJob(jobID);
    }else{
        Database.executeBatch(this, 50);            
    }
        
}

public database.QueryLocator start(database.BatchableContext BC){                               
    String query = 'SELECT Id, POP_User__c FROM User where Name not in (\'Chatter Users\')';
    return database.getQueryLocator(query);
}

public void execute(Database.BatchableContext BC, List<User> scope){
    //Update User information
    List<PermissionSetAssignment> listPop = [SELECT Id, AssigneeId FROM PermissionSetAssignment WHERE PermissionSet.Name = 'POP_User'];
    Set<Id> setPopUser= new Set<Id>();
    for(PermissionSetAssignment item : listPop ){
        setPopUser.add(item.AssigneeId);
    }
    
    for(User item : scope){
        if(setPopUser.contains(item.Id)){
            item.Pop_User__c= true;
        }else{
            item.Pop_User__c= false;
        }
    }
    update scope;
}

public void finish(database.BatchableContext BC){       
     
}

}

And here is my Test Class

@isTest 
public class Schedule_Batch_CheckUser_Test {
private static String CRON_EXP = '0 30 * * * ?';
@testSetup
static void initData(){
    Profile pp = [SELECT Id FROM Profile WHERE Name='System Administrator'];
    
    User u = new User();
    u.email = 'username2099NQG@pop.com';
    u.CompanyName = 'TEST';
    u.Lastname = 'Last';
    u.Department = 'Customer Success';
    u.Title = 'title';
    u.Alias = 'alias';
    u.TimeZoneSidKey = 'America/Los_Angeles';
    u.EmailEncodingKey = 'UTF-8';
    u.LanguageLocaleKey = 'en_US';
    u.LocaleSidKey = 'en_US';
    u.IsActive = true;
    u.username = 'username2099NQG@pop.com';
    u.ProfileId = pp.Id;
    insert u; 
            
    
    PermissionSet ps = [SELECT Id  FROM PermissionSet WHERE Name = 'Popuser' limit 1];
    
    PermissionSetAssignment psa = new PermissionSetAssignment
        (PermissionSetId = ps.Id, AssigneeId = u.Id);
    insert psa;  
}
@isTest
static void test1(){
    
                                         
    System.Test.startTest();                   
    
    Schedule_Batch_CheckUser a = new Schedule_Batch_CheckUser ();
    a.execute(null);        
                
    System.Test.stopTest();                
}

}

When I run the test class it return sucessfully but the coverage is just 21% and it only cover first if condition of the first execute method in this

public void execute(SchedulableContext SC){   
if(System.Test.isRunningTest()){
    ID jobID = Database.executeBatch(new Schedule_Batch_CheckUser(), 50);
    System.abortJob(jobID);
}

only this part is cover. Can you help me why I get this ?

I have follow this to trying to fix it but not working yet. https://developer.salesforce.com/forums/?id=906F0000000936mIAA

7
  • Will this help https://salesforce.stackexchange.com/questions/174732/no-more-than-one-executebatch-can-be-called-from-within-a-test-method. I already faced this issue in my initial phase in salesforce – Nagendra Singh Nov 26 '20 at 8:06
  • Thanks for your quickly response, however if I add that condition in the query nothing change, the coverage is still 21%. I have try to keep that condition in query and remove isRunningTest checking on the Execute method, but I got the error No more than one executeBatch can be called from within a test method. – elevven Nov 26 '20 at 8:14
  • Ok so now you are not facing No more than one executeBatch can be called from within a test method. but your coverage is 21%. You need to check which lines are getting covered and check if lines are not getting covered then why is that so. what condition will allow those lines to get covered. Also why is this line written System.abortJob(jobID); – Nagendra Singh Nov 26 '20 at 8:24
  • So when I try to find the solution for my problem I have seen someone said I should using that condition to run the testclass. If I remove that line System.abortJob then the testclass will return failed with the error No more than one executeBatch can be called from within a test method – elevven Nov 26 '20 at 8:31
  • After removing System,abortJob replace your query with this 'SELECT Id, POP_User__c FROM User where Name not in (\'Chatter Users\')' +(Test.isRunningTest()?' LIMIT 50':''); – Nagendra Singh Nov 26 '20 at 8:38
0

After removing System.abortJob

Replace your query with 'SELECT Id, POP_User__c FROM User where Name not in (\'Chatter Users\')' +(Test.isRunningTest()?' LIMIT 50':''); and it should work.

This is because you're querying on a Setup object (one not affected by SeeAllData=false), so you'll need to limit your query. So all your records gets executed within a single batch.

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.