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
Dev_AryaDev_Arya 

Test Class : SeelAllData=false still select returns all the records

Hi All,

I have a batch class which retrieves user records and updates the users email. In my test class, I created 10 users and called the batch class. All good till here, but when I requery the user records to assert, it returns all the records from the org. This is strange to me. Could some explain me this behaviour. 
Batch class
/**
 * Created by darya on 1/2/2018.
 */

global class UpdateQAEmailsBatch implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC) {

        List<Id> profileIds = new List<Id>();
        List<String> profileNameStrings = new List<String>{'System Administrator','Profile B','Profile C'};
        for (Profile p :  [SELECT Id FROM Profile WHERE Name in: profileNameStrings])
        {
            profileIds.add(p.Id);

        }
        String[] emailFilters = new String[]
        {
                '%@company.com'
        };
        String query = 'SELECT Id, Name, Email, ProfileId FROM User WHERE Email like :emailFilters AND ProfileId NOT IN :profileIds';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<User> scope) {
        for (User usr : scope)
        {
            String email = usr.Email;
            email = email.replace('@', '=') + '@example.com';
            usr.Email = email;
        }
        update scope;
    }

    global void finish(Database.BatchableContext BC) {
        //TODO: Send Email
    }
}

Test class
/**
 * Created by darya on 1/3/2018.
 */
@isTest
public with sharing class UpdateQAEmailsBatchTest {

    @TestSetup static void setup() {

        List<User> users = new List<User>();
        List<Id> profileIds = new List<Id>();
        List<String> profileNameStrings = new List<String>{'Profile C','Profile D','Profile E','Profile F','Profile G','Profile H'};
        for (Profile p :  [SELECT Id FROM Profile WHERE Name in: profileNameStrings])
        {
            profileIds.add(p.Id);

        }

        for (Integer i = 0; i < 10; i++)
        {
            User user = new User(alias = 'testQA' + i,
                    email = 'testUserQAUpdate' + i + '@company.com',
                    LastName = 'testUserQAUpdate' + i,
                    Username = 'testUserQAUpdate' + i + '@company.com',
                    emailencodingkey = 'UTF-8',
                    languagelocalekey = 'en_US',
                    localesidkey = 'en_US',
                    profileid = profileIds[(math.random() * (profileIds.size())).intValue()],
                    country = 'United States',
                    IsActive = true,
                    CompanyName = 'Getinge France',
                    Getinge_Company__c = 'Getinge',
                    timezonesidkey = 'America/Los_Angeles');
            users.add(user);
        }
            insert users;
    }

    static testmethod void executeBatch() {

        UpdateQAEmailsBatch batch = new UpdateQAEmailsBatch();

        Test.startTest();
            Database.executeBatch(batch);
        Test.stopTest();
        List<User> users = [SELECT Id,Email FROM User];
        System.debug('Here its returns all the records from the Org. It should only return the above created 10 records');
        System.debug('users retrieved count: '+ users.size());
        for (User user : users)
        {
            System.debug('Id: ' + user.Id + '   Email:  ' + user.Email);
            Integer index = ((String) user.Email).indexOf('@');
            System.assertEquals(((String) user.Email).substring(index, index + 12), '@example.com');
        }
    }
}
Thanks in advance.
 
Best Answer chosen by Dev_Arya
v varaprasadv varaprasad
Hi Dev,

Some Objects retrieve all org data:
More info :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm

If you want to retrieve only inserted records then you can add all ids to set or Run your code with system.runas();

In system.runas() insert all records and check once I think it will return only 10 records now.


Hope this helps you.

Thanks
Varaprasad


 

All Answers

v varaprasadv varaprasad
Hi Dev,

Some Objects retrieve all org data:
More info :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm

If you want to retrieve only inserted records then you can add all ids to set or Run your code with system.runas();

In system.runas() insert all records and check once I think it will return only 10 records now.


Hope this helps you.

Thanks
Varaprasad


 
This was selected as the best answer
Dev_AryaDev_Arya
Once again thanks Varaprasad. :-)