function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TanyrilTanyril 

User reference in test class is returning all users instead of just the test user

Here is the test class:

@isTest
public static void testBatch() {

Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 

User u2 = new User(Alias = 'standt', Email='standarduser@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
    insert u2;

 
 Account a = new Account(Name = 'Test Account', ShippingPostalCode = '00000', BillingCountry = 'US', OwnerID= u2.Id, Inflows__c = 100, Outflows__c= 100, navmfv2__AUM_as_of__c = Date.today());
    insert a;

    Test.StartTest();
    FlowstoUser BatchClass = new FlowstoUser();
    Database.executeBatch(BatchClass, 1);
    Test.StopTest();
  }

 Here's the error from test execution logs:

 

13:43:38.595 (2595524000)|CODE_UNIT_STARTED|[EXTERNAL]|01pZ00000005mN5|FlowstoUser
13:43:38.604 (2604439000)|SOQL_EXECUTE_BEGIN|[13]|Aggregations:0|Select Id, Inflow_of_Owned_Accounts__c, Outflow_of_Owned_Accounts__c  from User
13:43:38.641 (2641706000)|SOQL_EXECUTE_END|[13]|Rows:18
13:43:38.643 (2643968000)|EXCEPTION_THROWN|[EXTERNAL]|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.

 

As you can see, it's returning all of the users in the system PLUS the test user instead of just the test user. Any help would be greatly appreciated.

bob_buzzardbob_buzzard

User data isn't isolated from tests, so you will get all users back from your query, see:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

 

Can you post the code that is actually causing the error to be thrown.

TanyrilTanyril

Sure.

 

global with sharing class FlowstoUser implements Database.Batchable<sObject> {
   global final String Query;

   global FlowstoUser() {
        this.Query = 'Select Id, Inflow_of_Owned_Accounts__c, Outflow_of_Owned_Accounts__c  from User'; 
   }
   
   global FlowstoUser(String query) {
        this.Query = query;
   }

   global Database.QueryLocator start(Database.BatchableContext BC) {
      return Database.getQueryLocator(Query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope) {
    
    List<User> UserFlows = (List<User>) scope;
    List<User> UpdateUserFlows = new List<User>();
    
    for (User UserFlow : UserFlows) {
        
        String UID = UserFlow.Id; 
        List<AggregateResult> Posflow = new List<AggregateResult>([SELECT Sum(Inflows__C) flows FROM Account WHERE (OwnerID = :UID)]);
        List<AggregateResult> Negflow = new List<AggregateResult>([SELECT Sum(Outflows__c) flows FROM Account WHERE (OwnerID = :UID)]);
        for (AggregateResult pbal : Posflow) {       
            UserFlow.Inflow_of_Owned_Accounts__c = (Decimal)pbal.get('flows');
        }
        for (AggregateResult nbal : Negflow) {       
            UserFlow.Outflow_of_Owned_Accounts__c    = (Decimal)nbal.get('flows');
        }
        UpdateUserFlows.add(UserFlow);
    }
    
    update UpdateUserFlows;
    
   }

   global void finish(Database.BatchableContext BC){

   }
 
@isTest
public static void testBatch() {

Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 

User u2 = new User(Alias = 'standt', Email='standarduser@testorg.com', 
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
      LocaleSidKey='en_US', ProfileId = p.Id, 
      TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
    insert u2;

 
 Account a = new Account(Name = 'Test Account', ShippingPostalCode = '00000', BillingCountry = 'US', OwnerID= u2.Id, Inflows__c = 100, Outflows__c= 100, navmfv2__AUM_as_of__c = Date.today());
    insert a;

    Test.StartTest();
    FlowstoUser BatchClass = new FlowstoUser();
    Database.executeBatch(BatchClass, 1);
    Test.StopTest();
  }
}

 How can I create a successful test class if I can't isolate my test user? The idea is it would be a batchable class to sum up the inflow and outflow fields on the accounts they own.

TanyrilTanyril

I got around it by setting up an If statement in my query formation that checks to see if test data exists. Like so:

   global FlowstoUser() {
     List<User> Tcheck = [select alias from user where TestNum__c = 100101001];
     If(Tcheck.isEmpty()){this.Query = 'Select Id, Inflow_of_Owned_Accounts__c, Outflow_of_Owned_Accounts__c  from User';}
    else{this.Query = 'Select Id, Inflow_of_Owned_Accounts__c, Outflow_of_Owned_Accounts__c  from User where TestNum__c = 100101001'; }
    
   }