Activity Forums Salesforce® Discussions In what condition we apply the soql query in test class?

  • Ravi

    Member
    July 28, 2016 at 8:02 am

    Hi Mohit,
    First scenario where we use SOQL query is to retrive some organization data from salesforce database. In this case you have to make your test class all data visible using (SeeAllData = true). But using this is not recommended since your yout test class may not work in other org.

    // All test methods in this class can access all data.
    @isTest(SeeAllData=true)
    public class TestDataAccessClass {
    // This test accesses an existing 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);
    }

    Other scenario where we use SOQL query is to verify the inserted/updated record.

    public class TestDataAccessClass {

    // This test creates and accesses a new test account.
    static testmethod void myTestMethod1() {
    // Create a test account .
    Account testAccount = new Account();
    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);
    }

    }

    • This reply was modified 7 years, 8 months ago by  Ravi.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos