Monday, September 10, 2018

Test class in salesforce

Why we require a test class in Salesforce?

  • When we are moving code from sandbox to production org we need to write test class.
  • Whatever code we have written we need to ensure that each and every case is correct and we can create a dummy data for it.
  • The code must have 75% coverage to deploy it to production.

Points to remember.


  • As every organization have code limit by writing @isTest above test class, the limit will not be considered.
  • Every test class has a method declared with testmethod keyword and is static.   
                  Syntax: static testmethod void methodname() 
                                 {

                                    }            
      • Test class does not have access to organization data but if we write @isTest(seeAllData=true),then we can access data in the test class as well.


      • If we are defining a test class with @isTest(SeeAllData=true) then we can access data in test class from database under all method present in test class.Under this case annoting a method with @isTest(SeeAllData=false) would be ignored and you can access database data inside this method as well. 

      • If we are defining a test class with @isTest(SeeAllData=false) then we cannot access data in test class from database under all method present in test class.Under this case annoting a method with @isTest(SeeAllData=true) would not be ignored and you can access database data inside this method. 
      • Test.starttest() and Test.stoptest are standard method available with test class.
      • Whatever test we are performing we write the test inside of Test.starttest() and Test.stoptest, Basically they help to create fresh set of governing limit.
      • Each test method can call start test and stop test only once.
      • @testSetup is used to create test records and we can use this records in test methods in same test class, No need to create records multiple times in different methods.
      • test.isRunningtest() is used to bypass condition in apex class if we are not able to satisfy certain if conditions in apex class.
      • We should always considered doing testing with bulk records.
      • We Should test "test cases" for both positive and negative test scenario.

      Create a sample test class for below trigger.


      Trigger:

      trigger contactnamevalidation on Contact(before update)
      {
      for(contact obj:trigger.new)
      {
      if(obj.FirstName=='test1')
      {
      obj.FirstName='test2';
      }

      }

      }

      Test class for above trigger:

      @isTest

      Public class testclass
      {
       static testmethod void testmethodname()  // No parameter inside test method
       {

       contact obj=new contact();
       obj.firstname='test1';
       obj.lastname='data';
       test.starttest();
       insert obj;

       obj.lastname='data1';
       update obj;
       test.stoptest();


       }

      }
      **************************************************************************************************************
      TAGS:test class in salesforce example,test class in salesforce best practices,test classes in salesforce,test class for trigger in salesforce example.
      **************************************************************************************************************

      No comments:

      Post a Comment