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
sakthidasan salesfrocesakthidasan salesfroce 

Test class SeeAllData

Hi ,I'm new to salesforce  I have doubt on Test class if use SeeAllData == true will give access to all records in your org,but every one said.suppose it's move to the production it might be failure.what kind of issue will occure Please explain.
Mahesh DMahesh D
Use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization,

1) If a test class is defined with the isTest(SeeAllData=true) annotation, this annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the testmethod keyword

2) The isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level

Use the isTest(SeeAllData=false) means you need to create the test data in your org.

User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
SeeAllData=true will not work for API 23 version eailer .



NOTE:- Always try to avoid use SeeAllData= true because in that case your test class depend on  your sandbox data. You May get some issue will deployment.

----------
 
SeeAlldata = true , is nothing but referring to the org data as opposed to creating test data in test class. 
Is it best practice to do it ? - No
Is it ok to do it ? Yes . But your test class may fail when deploying to other org , because the data in the destination org may not comply 
Can we avoid doing it ? Yes . Create test data in your test class
Then why do we need this ? Salesforce has some objects for which we cannot create test data , in which case you need to use org data , to cover your code. 


Annotate your test class or test method with IsTest(SeeAllData=true) to open up data access to records in your organization.

Please go through thte below links:

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

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

http://salesforce.stackexchange.com/questions/53998/test-class-seealldata-true-annotation

http://salesforce.stackexchange.com/questions/42026/see-all-data-and-sharing-rules

http://www.laceysnr.com/seealldata-why-i-think-you-shouldnt-use/


Please do let me know if it helps you.


Regards,
Mahesh
 
Mahesh DMahesh D
Hi sakthidasan,

Apart from these above points, if you use the SeeAllData=true. The SOQLs will return all the data exists in Production and some times our test classes may fail if the data what we are expecting is not currently available in Production.

Example:

If you have a Test Class on Case object where it is checking / testing based on Case Status = 'New' and if you relay on Production data and if you don't get a single record also as part of SOQL query execution then our Test class may not execute fully orelse may fail.
Hence using the SeeAllData=false and making the required data in the Test Method itself is the best practice.

Hope this is helpful.


Please do let me know if it helps.

Regards,
Mahesh
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for more information on same
1) http://amitsalesforce.blogspot.com/2015/09/test-classes-with-istest.html
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Using the isTest(SeeAllData=true) Annotation
Annotate your test class or test method with IsTest(SeeAllData=true) to open up data access to records in your organization.
This example shows how to define a test class with the isTest(SeeAllData=true) annotation. All the test methods in this class have access to all data in the organization
// All test methods in this class can access all data.
@isTest(SeeAllData=true)
public class TestDataAccessClass {

    // This test accesses an existing account. 
    // It also creates and accesses a new test account.
    static testmethod void myTestMethod1() {
        // Query an existing account in the organization. 
        Account a = [SELECT Id, Name FROM Account WHERE Name='Acme' LIMIT 1];
        System.assert(a != null);
        
        // Create a test account based on the queried account.
        Account testAccount = a.clone();
        testAccount.Name = 'Acme Test';
        insert testAccount;
        
        // Query the test account that was inserted.
        Account testAccount2 = [SELECT Id, Name FROM Account 
                                WHERE Name='Acme Test' LIMIT 1];
        System.assert(testAccount2 != null);
    }
       
    
    // Like the previous method, this test method can also access all data
    // because the containing class is annotated with @isTest(SeeAllData=true).
    @isTest static void myTestMethod2() {
        // Can access all data in the organization.
   }
  
}



IsTest(SeeAllData=true) Annotation
use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization,

1) User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .

2) In test class and methods, we can access data in the organization. Prior to it we can just access test data. Using this we can access organization data. For Apex code saved using Salesforce.com API version 24.0 and later, use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization, including pre-existing data that the test didn’t create.
Starting with Apex code saved using Salesforce.com API version 24.0, test methods don’t have access by default to pre-existing data in the organization. However, test code saved against Salesforce.com API version 23.0 or earlier continues to have access to all data in the organization and its data access is unchanged.

3) If a test class is defined with the isTest(SeeAllData=true) annotation, this annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the testmethod keyword.

4) The isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level.
However, using isTest(SeeAllData=false) on a method doesn’t restrict organization data access for that method if the containing class has already been defined with the isTest(SeeAllData=true) annotation. In this case, the method will still have access to all the data in the organization


SeeAllData=true - Why I Think You Shouldn't Use It
http://www.laceysnr.com/seealldata-why-i-think-you-shouldnt-use/
http://opfocus.com/demystifying-seealldata-in-unit-tests/

So your seeAllData= true is depend on your sandbox data so please try to avoid because at the time deployment you will get error.


When to use SeeAllData=true
So now that we have established that SeeAllData=true is evil and should be avoided at all costs;
1) If you need access to a custom Price Book in your Unit Test, then you have two choices. Either re-create the custom Price Book in the same way that you create other dummy data for testing. Or, if necessary, use SeeAllData=true to access the Custom Price Book.

2) Access to ActivityHistory records. Strangely enough, if you are writing a Unit Test that creates an Activity on some Object, say Opportunity, then you would expect to see the ActivityHistory record in the database when you perform a query from your test code. However, the ActivityHistory records are not returned without SeeAllData=true. being set to true. This seems inconsistent as we can query for the Opportunity and find it in the database. Turning on SeeAllData=true solves the problem

Please let us know if this will help you