Unit Testing in Salesforce

Testing is a key and critical component to successful long term software development process. Salesforce.com strongly recommends using a test-driven development process which occurs at the same time as code development. Salesforce has very strong set of documentation. When I was learning salesforce unit testing, I realize that it is difficult to understand where to start read. Therefore, I summarized the unit testing for salesforce beginners to understand the basic aspects of unit testing.

There are few things to consider before you deploy or upload the code or package;
  • 75% of your Apex code must be covered by unit tests
  • All the tests must complete successfully
  • Every trigger has some test coverage (1%)
  • All classes and triggers must compile successfully
When you are writing a test class, you have to write test for Single Action, Bulk Action, Positive Behavior, Negative Behavior, and Restricted User.
  • Single Action :Test to verify that a single record produces the correct, expected result.
  • Bulk Action :  Test not only the single record case, but the bulk cases as well
  • Positive Behavior :  Verify that the expected behavior occurs through every expected permutation
  • Negative Behavior :  Verify that the error messages are correctly produced 
  • Restricted User :Test whether a user with restricted access to the sObjects used in     your code sees the expected behavior 
Test Class can be defined @isTest annotation. Before the Winter 12’ release we had only private test classes, but on Winter 12’ release salesforce has given the chance to write public test classes as well. Salesforce has released the public test classes for expose common methods for data creation. It can be used to setting up data that the tests need to run against. Public test methods can be called from a running test but not from a non-test request.

When you create a test method,
  • Use static
  • Use testMethod keyword 
  • Use void return type
  • No any arguments
  • No data changes performed in a test method
  • Don’t send emails
  • Cannot be used to test Web service callout because web services are asynchronous and tests are synchronous.
    • The API for asynchronous test runs is a Beta release (Winter ‘12)
    • For More : Force.com Apex code Developer’s Guide Page 153 
The key methods to use in your unit tests are the system.assert() methods. There are three types of system.assert() methods.
  • System.assert(condition)
  • System.assertEquals(x,y)
  • System.assertNotEquals(x,y)
For the security review, every test method must have at least one system.assert() method. We need use assert methods not only for the pass the security review but also as a best practice. It will be help us to keep track the failures of Apex classes or triggers.

Structure of the Test Class
@isTest

private classTest_class
{
    public static testMethodvoid test_name()
    {
       //code_block;
    }
}
Structure of public Test class for test data creation 
@isTest
public class TestUtil
{
    public static void createTestAccounts() 
    {
      // Create some test accounts
    }

    public static void createTestContacts()
    {
      // Create some test contacts
    }
}

Comments

  1. Hey Chamil,

    I am new apex and visualforce. I have simple question. Why we write "static" keyword for test methods.

    Thanks in advance.

    ReplyDelete
  2. because you do not chnage

    ReplyDelete
  3. Test methods are static so that they can be executed or called by using their class name. Static method can be access directly by using their class name.

    ReplyDelete
  4. nice content see this link to get http://www.salesforcetraining.in/ to get training from professionals

    ReplyDelete
  5. Thanks for sharing detailed post about saleforce unit testing. Whether salesforce will support functional testing. Let me know admin.

    ReplyDelete
  6. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!

    Salesforce Training Institutes in Chennai

    ReplyDelete
  7. Nice blog........... Salesforce training course is recommended in this cutting-edge competition. It is ideal for system administrators managing the configuration and maintaining the salesforce application in an organization.For more detailssalesforce online training in hyderabad

    ReplyDelete
  8. It’s really amazing that we can record what our visitors do on our site. Thanks for sharing this awesome guide. I’m happy that I came across with your site this article is on point,thanks again and have a great day. Keep update more information..

    Salesforce Training in Chennai


    AWS Training in Chennai

    ReplyDelete
  9. This article helps me to know about the essential skills for learning the Cloud Computing and keep on updating us. This content is really helped me to know about the importance of Cloud Computing. Cloud Computing Training in Chennai | Cloud Computing Training | Cloud Computing Courses

    ReplyDelete
  10. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  11. Diac Offers the best Salesforce-CRM Certification training in Noida by experts and get Salesforce developer certification, Call 9310096831. Take courses to learn the ins and outs of Salesforce, including prospecting, data migration, admin essentials, and much more.

    ReplyDelete
  12. It was an excellent article to hear from you which is very useful. Thank you for sharing the details about Salesforce Unit Testing. We also offer efficient yet affordable Salesforce Service Cloud Training with quality as the main objective.

    ReplyDelete
  13. Excellent way of writing and expressing your thoughts and ideas to the readers, very much impressed in your article. Keep doing more, waiting to read your next blog.
    Regards:
    Software training institutes in chennai
    Software testing training institutes in chennai

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. This is my first time i visit here. I found so many interesting points in your content From the lots of comments on your Blog, I guess I am not the only one having all updates here! keep doing great job.
    Salesforce Training Chennai
    salesforce developer training in chennai

    ReplyDelete
  17. Thanks For Posting About Salesforce Can you Please Explain can We Integrate Selenium With Salesforce.

    ReplyDelete

  18. this blog providing good information To learn

    HTML PHP NodeJS Hadoop Training in Tarnaka Hyderabad

    contact: infotechiesguild@gmail.com

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Awe! What An Blog Very Helpful and interesting Really A great center for acquiring knowledge.Very Helpful Post And Explained Very Clearly About All the things.Very Helpful. Coming To Our Self We Provide Restaurant Equipment Parts .Really Thankfull For the blogger providing such a great information.Thank you. Have a Nice Day.

    ReplyDelete
  21. I really enjoy the blog.Much thanks again. Really Great. salesforce Online Training Hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

Parameter passing using Javascript+actionFunction in visualforce

Displaying pop-up summaries on hover in visualforce