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
Mayank_SareenMayank_Sareen 

Confusion with @isTest(SeeAllData=false)

Hi,

I was reading about testing in apex. I have a doubt with @isTest(SeeAllData=true). Now lets say i have a class with around 20 methods and i want to see data for all the methods in a class except for one or two. If I use @istest(SeeAllData=false) on a particular method it is not working. How can i achieve this. Is there a way of achieving this without specifying for individual methods .

Thanks
Mayank.
Best Answer chosen by Mayank_Sareen
Akshay_DhimanAkshay_Dhiman
Hi Mayank,

Why not to use (SeeAllData == true) in Test class

This is because of the following reason -

1. isTest(SeeAllData=true) Annotation your test class or test method with IsTest(SeeAllData=true) to open up data access to records in your organization
2. Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access
by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings
data, and can only access data that they create.
3.  You must create test data for each test. You can disable this restriction by annotating your test class or
test method with the IsTest(SeeAllData=true) annotation. For more information, see IsTest(SeeAllData=true)
Annotation.
4. sTest(SeeAllData=true) is used to open up data access to records in your organization.some time it can create the problem.
5. Also If you use the (SeeAllData=true), it might happen that test class pass in a sandbox but fails in production while deployment


You can use @testSetup Annotation for creating data for test class
*********************Example***********************************
@isTest
private class CommonTestSetup {

    @testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<2;i++) {
            testAccts.add(new Account(Name = 'TestAcct'+i));
        }
        insert testAccts;        
    }
    
    @isTest static void testMethod1() {
        // Get the first test account by using a SOQL query
        Account acct = [SELECT Id FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Modify first account
        acct.Phone = '555-1212';
        // This update is local to this test method only.
        update acct;
        
        // Delete second account
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // This deletion is local to this test method only.
        delete acct2;
        
        // Perform some testing
    }

    @isTest static void testMethod2() {
        // The changes made by testMethod1() are rolled back and 
        // are not visible to this test method.        
        // Get the first account by using a SOQL query
        Account acct = [SELECT Phone FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Verify that test account created by test setup method is unaltered.
        System.assertEquals(null, acct.Phone);
        
        // Get the second account by using a SOQL query
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // Verify test account created by test setup method is unaltered.
        System.assertNotEquals(null, acct2);
        
        // Perform some testing
    }

}



Please mark as best answer if it helps you.

Thank You

 

All Answers

Shubham NandwanaShubham Nandwana

Hi Mayank,
if the containing class has been annotated with @isTest(SeeAllData=true), annotating a method with @isTest(SeeAllData=false) is ignored for that method. In this case, that method still has access to all the data in the organization. Annotating a method with @isTest(SeeAllData=true) overrides, for that method, an @isTest(SeeAllData=false) annotation on the class.

So you should not give @isTest(SeeAllData=true) for a class but only for methods and moreover while testing you should create your own data and should not use org data.
Refer to https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm for testing best practices.

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts

Ajay K DubediAjay K Dubedi
Hi Mayank,

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.

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

Thank You
Ajay Dubedi
Akshay_DhimanAkshay_Dhiman
Hi Mayank,

Why not to use (SeeAllData == true) in Test class

This is because of the following reason -

1. isTest(SeeAllData=true) Annotation your test class or test method with IsTest(SeeAllData=true) to open up data access to records in your organization
2. Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access
by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings
data, and can only access data that they create.
3.  You must create test data for each test. You can disable this restriction by annotating your test class or
test method with the IsTest(SeeAllData=true) annotation. For more information, see IsTest(SeeAllData=true)
Annotation.
4. sTest(SeeAllData=true) is used to open up data access to records in your organization.some time it can create the problem.
5. Also If you use the (SeeAllData=true), it might happen that test class pass in a sandbox but fails in production while deployment


You can use @testSetup Annotation for creating data for test class
*********************Example***********************************
@isTest
private class CommonTestSetup {

    @testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<2;i++) {
            testAccts.add(new Account(Name = 'TestAcct'+i));
        }
        insert testAccts;        
    }
    
    @isTest static void testMethod1() {
        // Get the first test account by using a SOQL query
        Account acct = [SELECT Id FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Modify first account
        acct.Phone = '555-1212';
        // This update is local to this test method only.
        update acct;
        
        // Delete second account
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // This deletion is local to this test method only.
        delete acct2;
        
        // Perform some testing
    }

    @isTest static void testMethod2() {
        // The changes made by testMethod1() are rolled back and 
        // are not visible to this test method.        
        // Get the first account by using a SOQL query
        Account acct = [SELECT Phone FROM Account WHERE Name='TestAcct0' LIMIT 1];
        // Verify that test account created by test setup method is unaltered.
        System.assertEquals(null, acct.Phone);
        
        // Get the second account by using a SOQL query
        Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
        // Verify test account created by test setup method is unaltered.
        System.assertNotEquals(null, acct2);
        
        // Perform some testing
    }

}



Please mark as best answer if it helps you.

Thank You

 
This was selected as the best answer