Showing posts with label Test class in salesforce. Show all posts
Showing posts with label Test class in salesforce. Show all posts

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.
      • operation done on the records from @testSetup are local to that test method only in test class.

      TEST CLASS BEST PRACTISES:


      • We should always considered doing testing with bulk records.


      • We Should test "test cases" for both positive and negative test scenario.


      • Avoid using (SeeAllData=true) in test class because it might happen that the test class pass in sandbox but fails in production.


      • Avoid using hardcoded is's in  test class.


      • Each test method can call start test and stop test only once, but number depends on the number of test methods in test class.

      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 using @testSetup:



      @isTest
      Public class testclass
      {
       @testSetup
       static void testsettingup()
       {
       contact obj=new contact();
       obj.firstname='test';
       obj.lastname='data';
       insert obj;

       }
       static testmethod void testmethodname() //No parameter inside test method
       {
       test.starttest();
       contact obj1=new contact();
       obj1=[Select id,firstname from contact where firstname='test'];

       obj1.firstname='test1';
       update obj1;
      // operation done on the records from @testSetup are local to this test method only.
       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.