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
LinThawLinThaw 

SeeAllData on User Object

Hi there,

I am trying following test class with api version 46.
my expected result is as follow.
totalUser = 0 and totalAccount = 0
But totalUser is not 0.
Any suggestion for using with @isTest(SeeAllData = false) User Object?
@isTest(SeeAllData = false)
public class TestClassSample {
    
    private static testMethod void test01() {      
        
        Test.startTest();
        
        System.debug('Total User Record = ' + [Select Count(Id) totalUser From User]);
        
        Test.stopTest();  
    }    
    
    private static testMethod void test02() {      
        
        Test.startTest();
        
        System.debug('Total Account Record = ' + [Select Count(Id) totalAccount From Account]);
        
        Test.stopTest();  
    }
}


Thanks
Regards,
LinThaw
MKRMKR
Hi,

Unfortunately, users are not affected by @isTest(SeeAllData=false). See below link for additional information on which objects are supported and which not.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm

Regards,
MKR
pradeep kumar yadavpradeep kumar yadav
Hi Lin,

If you want to access org's original user data, in order to do that you have to make @isTest(SeeAllData = true).
But, it will applied to all methods in test class. You apply this on specific methods as well as shown below:-
@isTest(SeeAllData = false)
public class TestClassSample {
    
    @isTest(SeeAllData = true)
    static void test01() {      
        
        Test.startTest();
        
        System.debug('Total User Record = ' + [Select Count(Id) totalUser From User]);
        
        Test.stopTest();  
    }    
    
    @isTest
    static void test02() {      
        
        Test.startTest();
        
        System.debug('Total Account Record = ' + [Select Count(Id) totalAccount From Account]);
        
        Test.stopTest();  
    }
}
In the above code the debug will be
totalUser = total user in your org
totalAccount = 0
LinThawLinThaw
Thanks, I don't want to access org's original user data. I think there is no way as MKR's mention.
MKRMKR
Hi,

You can always anyway create users for your tests and use that data to ensure that the user is always in a shape that you want. These users are automatically removed when the test execution ends (like any other data created in test context).

Regards,
MKR
LinThawLinThaw
Thanks MKR, I am also thinking as you mention. if so we need to create test user data to meet with the conditions for soql using in apex class.
Piyush Gautam 6Piyush Gautam 6
Hi LinThaw,

You can create a test user in your test class.
Please follow the below code:
@isTest(SeeAllData = false)
public class TestClassSample {
    
    private static testMethod void test01() {   
		Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
				User u = 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 u;

				System.runAs(u) {
				  // Functionality to check with the test user
				}	
        
        Test.startTest();
        
        System.debug('Total User Record = ' + [Select Count(Id) totalUser From User]);
        
        Test.stopTest();  
    }    
    
    private static testMethod void test02() {      
        
        Test.startTest();
        
        System.debug('Total Account Record = ' + [Select Count(Id) totalAccount From Account]);
        
        Test.stopTest();  
    }
}

Thanks
LinThawLinThaw
Thanks Piyush, I know creating user and runAs using in test class.