You need to sign in to do that
Don't have an account?

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.
/* * **Created by Jack Wang 9.24.2014 */ global class AR_Creating75DummyNML implements Database.Batchable<sObject>{ static final Integer numofNML=75; global final String query; global AR_Creating75DummyNML(String q) { query=q; } public List<Task> createNML(List<ID> IDs) { integer size=IDs.size(); List<integer> ints=new List<integer>(); Integer numberDays = date.daysInMonth(Date.today().year(), Date.today().month()); for(integer i=0;i<75;i++) { double k=Math.random()*size; ints.add(k.intValue()); } string userid=null; User[] users=[select id from User where name='Jeremy Young']; if(users.size()>0) { userid=users[0].id; } List<Task> tsks=new List<Task>(); for(Integer i : ints) { double datek=Math.random()*numberDays; Task nml=new Task(ownerid=userid,CallType__c='NML',whatid=String.valueOf(IDs[i]),Subject='NML', Status='Completed',Priority='Normal',ActivityDate=Date.today().tostartofMonth().addDays(datek.intValue())); tsks.add(nml); } return tsks; } global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Account> scope) { Map<String,List<ID>> territoryClientIDMap=new Map<String,List<ID>>(); for(Account client : scope) { if(territoryClientIDMap.containsKey(client.client_territories__c)) { territoryClientIDMap.get(client.client_territories__c).add(client.Id); } else { territoryClientIDMap.put(client.client_territories__c, new List<ID>()); territoryClientIDMap.get(client.client_territories__c).add(client.Id); } } for(List<ID> clientIDS : territoryClientIDMap.values()) { List<Task> tsks=createNML(clientIDS); insert tsks; } } global void finish(Database.BatchableContext BC) { } }
@isTest private class Test_AR_Creating75DummyNML { static testMethod void Test(){ //create 10 advisors ////for each advisor create 100 clients // string userid=null; User[] users=[select id from User where name='Jeremy Young']; if(users.size()>0) { userid=users[0].id; } List<Account> advisors=new List<Account>(); For(integer i=0;i<10;i++) { Account a=new Account(name='a'+String.valueOf(i),client_territories__c='a'+String.valueOf(i),account_type__c='provider',Monthly_Activity_Reporting__c=true,Account_Status__c='active'); advisors.add(a); } insert advisors; For(integer j=0;j<advisors.size();j++) { List<Account> clients=new List<Account>(); For(integer i=0;i<100;i++) { Account client=new Account(name=advisors[j].name+String.valueOf(i),client_territories__c=advisors[j].client_territories__c,account_type__c='prospect'); clients.add(client); } insert clients; } List<Account> advisors1= [select client_territories__c from Account where (account_type__c='provider' or account_type__c='advisor') and Monthly_Activity_Reporting__c=true and Account_Status__c='active']; List<String> territories=new List<String>(); List<String> advisorIDs=new List<String>(); for(Account advisor : advisors1) { territories.add(advisor.client_territories__c); advisorIDs.add(advisor.id); } //[select id,client_territories__c from Account where client_territories__c in :territories]; string tempquery='Select id,client_territories__c from Account where client_territories__c in ('; for(String terr : territories) { tempquery+='\''; tempquery+=terr; tempquery+='\','; } tempquery=tempquery.substring(0, tempquery.length()-1); tempquery+=') and id not in ('; for(String adid : advisorIDs) { tempquery+='\''; tempquery+=adid; tempquery+='\''; tempquery+=','; } string query=tempquery.substring(0, tempquery.length()-1); query+=')'; Test.startTest(); AR_Creating75DummyNML c=new AR_Creating75DummyNML(query); Database.executeBatch(c); Test.stopTest(); List<Task> tsk=[select ownerid,CallType__c,Subject, Status,Priority,ActivityDate from Task where createddate=TODAY and CallType__c='NML' and ownerid= :userid and Status='Completed' and Priority='Normal']; //75 tasks for each advisor System.assertEquals(750, tsk.size()); } }
I tried to google this error, but I didn't get a satisfied answer.
All Answers
Do you mean :
While this does remove the error about System.UnexpectedException error, my batch ran the constructor and start method, but never entered the execute method, meaning all of that code was not covered, and my assertions failed after the Test.stopTest();
What DID work for me was to call Database.executeBatch with a scope large enough so that only one execute() method call runs during the test context. You may have to adjust some of your batch logic if it is meant to be called with a small number of records and you have more records returned from your start query that can fit in one batch execution context.
For me, that meant: Since I had created two case records for my unit test, and the query ran through all cases.
Hope that helps!
Nathan
Here is the essential class that is being tested. Application__c is a custom object that has a Master-detail relationship with Account (or schools). The goal is to grant users/employees of the school Accounts permissions to Contacts that have Applications connected to those school Accounts. So College counselors can see the student data for applicants. Note that it and the earlier version of Student Term based permissions both used a Community service class that creates groups: And finally the test class that is throwing this error on validate. Note that it is using the executeBatchOnDemand method. (SeeAllData=true) is used in order to pull valid Group and Account information.
But when you use SeeAllData that won't work. So if you must use SeeAllData (and I would really try and see if you can avoid that), then the other way is to limit the records that are returned by the QueryLocator start method to only return records in a test context that will fit within the scope, which I assume is 200, To do that, I would usually create some test records with an identifieable name, and then add something to the query using Test.isRunningTest() so you know to add this just for tests.
so somthing like
To make that work, you might have to change the query to be a dynamic query using a string, but the bind variable will still work with that format.