Introduction to Test Classes in Salesforce

Whenever you want to push your code from sandbox to production? You need to write a test class first!, to ensure that your code should not break running application on production.

Note: Salesforce requires at least 75% of your code to be “tested” before deploying code into production org.

Important points to note down before writing TestClass:
  1. Whatever data we created in test class that won’t save into database. (delete all the data once transaction is completed)
  2. Do not write any SOQLs in your test class.
  3. Do not try to use “seeAllData=true”, Create your own dataset in test class.
  4. Use As many as Assertions like “System.AssertEquals” or “System.AssertNotEquals”
  5. To reset the Salesforce Governing limits for the particular transcation use Test.startTest() and Test.stopTest()
  6. If you want to access private method of the class which you want to test from your test class mark that method as @TestVisible
  7. Try to make your test class as private class
  8. Test in bulk: Test to see if your code can run on 200 records at once.
  9. Avoid Using Hard Coding Ids anywhere in test Class
  10. Use System.runAs() method to test the functionality in user Context


private class YourClassName_Demo {
 public static testMethod doTest() {
          // Write your logic of test class.

Note: Always remember that your test method must be static and have keyword “testMethod” in the signature of your method.


Setup > Develop > Apex Classes > New

Now let’s try to guess what this test code is doing. It may seem like a lot, but most of it is just nonsense you can ignore. Remember – – please focus on the green highlighted lines!

private class TestForceForecasting {
    public static testMethod void insertNewUser() {
        User userToCreate = new User();
        // Do you recognize these fields?
        userToCreate.FirstName = ‘salesforce’;
        userToCreate.LastName = ‘drillers’;
        userToCreate.Email = ‘’;
        userToCreate.Username = ‘’;
        userToCreate.Alias = ‘sdrillers’;
        userToCreate.ProfileId = ’00ei0000000rTfp’;
        // Don’t worry about these
        userToCreate.TimeZoneSidKey = ‘America/Denver’;
        userToCreate.LocaleSidKey = ‘en_US’;
        userToCreate.EmailEncodingKey = ‘UTF-8’;
        userToCreate.LanguageLocaleKey = ‘en_US’;
        insert userToCreate;
StartTest and stopTest Method:


Mark the point in your test class from where your test actually begins. Use this method when you want to bypass your Salesforce governing limits.


Mark the point in your test class from where you want your test ends. Use this method always in conjunction with startTest method.


  1. The Test.startTest method marks the point in your test class when your test logic actually begins. Every test method is allowed to call this method only once.
  2. All of the code before this method should be used to initialize the variables populates any data if required, and so on, allowing you to set up everything you need to run your actual test logic.
  3. Any apex code that executes after the call to the Test.startTest and before Test.stopTest method is assigned a new set of Salesforce governor limits.
  4. The Test.startTest method does not refresh the context of the test: it actually adds a context to your test. i.e., if your class makes 96 SOQL queries before it calls Test.startTest, and the first significant statement after Test.startTest is a DML statement, the program can now make an additional 100 queries. Once Test.stopTest is called, however, the program goes back into the original context, and can only make 4 additional SOQL queries before reaching the limit of 100 SOQL.
  5. All asynchronous calls which was made after Test.startTest method are collected by system automatically and when Test.stop method Test is executed, all asynchronous processes are run synchronously.
Subscribe Now