Vote 40  points

Tobias Forstmeier

Exclude SOQL Queries in Testclasses from Limits

Salesforce Platform, Apex & Visualforce, Developer Tools

Under Point Threshold

When Tests are executed and testing best practice is considered, each method invocation is checked with a System.assert method. To check the result of the transaction, a query must be executed after every action to get the updated data and verify the results.

So the best solution is, that SOQL queries inside a testmethod do not count again the limitation!

In bigger organizations with a complex data model every method needs relevant testdata to create before testing the actual data. While creating test data and relating the objects with master detail and lookups the dependent code executed with each dml by triggers is very huge. A number of queries is executed inside triggers with every DML statement which is not relevant for the test, but for the functionality of the organization.

@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.  
    
        // ==> THIS QUERY COUNTS AGAINST THE LIMITS BUT WILL NEVER BE EXECUTED DURING PRODUCTION PROCESSES - THIS QUERY MUST NOT RAISE THE SOQL COUNT
        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.
        // ==> THIS QUERY COUNTS AGAINST THE LIMITS BUT WILL NEVER BE EXECUTED DURING PRODUCTION PROCESSES - THIS QUERY MUST NOT RAISE THE SOQL COUNT
        Account testAccount2 = [SELECT Id, Name FROM Account
                                WHERE Name='Acme Test' LIMIT 1];
        System.assert(testAccount2 != null);
    }
}

3 years ago · 0 Comments ·Merge Idea · Report Abuse

1 to 4 of 4

Ideas

Apps

from AppExchange

Questions

Help us to keep IdeaExchange clean by pointing out overlapping ideas. We'll investigate your suggestion and merge the ideas if it makes sense.



 

 

Thanks for your merge suggestion. We will review it shortly and merge the ideas if applicable.

Salesforce.com takes abuse situations very seriously. Examples of abuse include but are not limited to posting of offensive language or fraudulent statements. To help us process your request as quickly as possible, please fill out the form below describing the situation. For privacy and security reasons, the final outcome of an abuse case may not be revealed to the person who reported it.


 

Thank you for your feedback. We take abuse seriously and will investigate this issue and take appropriate action.