In Salesforce it is most important to write test classes while deploying the code on Production. It helps us to do not write unused code in apex classes. While writing test classes we need to remember these testing principles.

To deploy to production at-least 75% code coverage is required, but your focus should not be on the percentage of code that is covered. Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This should lead to 75% or more of your code being covered by unit tests.

1. @isTest must be used before starting the test class if the class version is more than 25.

2. To define the test method, you can either use “@isTest” or “testMethod” keyword with method.

3. Stating with the salesforce API 28.0 test method cannot reside inside non test classes.

4. Always use @testSetup method dedicated to create test records for that class. This is newly added annotation and very powerful.

5. If possible Do not use seeAllData=true, Create your Own Test Data. SeeAllData=true will not work for API 23 version earlier. User, profile, organization, AsyncApexjob, Corntrigger, Record Type, Apex Class, Apex Component, Apex Page we can access without (seeAllData=true).

6. Use Test.startTest() to reset Governor limits in Test methods.

7. If you are doing any Asynchronous operation in code, then do not forget to call Test.stopTest() to make sure that operation is completed.

8. Use System.runAs() method to enforce OWD and Profile related testings. This is very important from Security point of View. System.runAs() will not enforce user permission or field level permission.

9. Use As much as Assertions like System.AssertEquals or System.AssertNotEquals.

10. Always test Batch Capabilities of your code by passing 20 to 100 records.

11. Always try to pass null values in every method. This is the area where most of program fails, unknowingly.

12. Please use call out mock to test web-service call out.

13. Maximum number of test classes run per 24 hours of period is not grater of 500 or 10 multiplication of test classes of your organization.

Test Method Syntax

Test methods take no arguments and have the following syntax:

@isTest static void testName() { 
  // code_block 

Alternatively, a test method can have this syntax:

static testMethod void testName() { 
  // code_block 

Test methods must be defined in test classes, which are classes annotated with isTest. This sample class shows a definition of a test class with one test method.

private class MyTestClass { 
  @isTest static void myTest() { 
   // code_block 

Example of a test class with Trigger and Apex class both:

Apex Trigger:

trigger HelloWorldTrigger on Book__c (before insert) {
  Book__c[] books =;

Apex Trigger Helper Class:

public class MyHelloWorld {
  public static void applyDiscount(Book__c[] books) {
   for (Book__c b :books){
    b.Price__c *= 0.9;

Test Class:

private class HelloWorldTestClass {
  static testMethod void validateHelloWorld() {
   Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
   System.debug('Price before inserting new book: ' + b.Price__c);
   // Insert book
   insert b;
   // Retrieve the new book
   b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];
   System.debug('Price after trigger fired: ' + b.Price__c);
   // Test that the trigger correctly updated the price
   System.assertEquals(90, b.Price__c);

Test Class for Standard Controller:

public class ExtensionTestClass {
  static testMethod void testMethod1() {
   Account testAccount = new Account();
   testAccount.Name='Test Account record' ;
   insert testAccount;

    ApexPages.StandardController sc = new      ApexPages.StandardController(testAccount);
  myControllerExtension testAccPlan = new myControllerExtension(sc);

    PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
    pageRef.getParameters().put('id', String.valueOf(testAccount.Id));

    //; call all your function here

Emizentech provides various salesforce development and salesforce consulting services for Salesforce app exchange, Paradot, Einstein, marketing cloud, IoT, and many more with the assistance of experienced salesforce developers. If you have a project in mind then let us know your requirements.

Avatar photo

With a decade of experience in eCommerce technologies and CRM solutions, Virendra has been assisting businesses across the globe to harness the capabilities of information technology by developing, maintaining, and improving clients’ IT infrastructure and applications. A leader in his own rights his teammates see him as an avid researcher and a tech evangelist. To know how the team Virendra can assist your business to adopt modern technologies to simplify business processes and enhance productivity. Let’s Talk.