• Home
  • About
  • Contact
  • Search
Cognitive
  • Facebook
  • Twitter
  • Gplus
  • How to Web Check-in

    Read more
  • Book Review: The Shiva Triology by Amish

    Read more
  • My review of Xiaomi MI4.

    Read more
PreviousNext
123
Salesforce.com, Technical

Testing Best Practices in Salesforce

When migrating your apex code from a Sandbox to a Production environment, the Force.com platform mandates you to ensure that your code  passes minimum 75% code coverage requirement while unit testing. This has been a best practice in the coding world for quite a long time for obvious reasons. Salesforce has made it a compulsion on their platform so that your code doesn’t misbehave once deployed to Production environment with live data.

In the ensuing write-up we will explore Testing Strategies – Common Pitfalls and Best Practices.

  • Test Classes run in parallel

When tests are initiated from the Salesforce user interface (including the Developer Console), test classes are run in parallel. Individual test methods inside test classes are run serially, but not necessarily sequentially. Now, although the test classes run in parallel by default to speed up test run time, sometimes parallel test execution could lead to data contention issues; typically if the tests are accessing your org data. See the following code sample:

Test Class 1

1A  @isTest (seeAllData = true)

2A  public class TestClass1 {

3A         private static testMethod void setAccount() {

4A                Account a= //Select a single account

5A                a.Name= ‘ABC’;

6A                update a;

7A                // check logic

8A           }

9A   }

Test Class 2

1B  @isTest (seeAllData = true)

2B  public class TestClass2 {

3B         private static testMethod void setAccount2() {

4B                Account a= //Select a single account

5B                a.Name= ‘XYZ’;

6B                update a;

7B                // check logic

8B           }

9B   }

 

In the above code, if lines 4A and 4B try to unfortunately lock the same account record from your database, this could lead to UNABLE_TO_LOCK_ROW error. To avoid this, firstly try to avoid using your org   data as test data. It is a best practice to create your own test data with the @testSetup for each test class. This data would be isolated per class and would only exist for the duration of the test run. Another option is to disable the parallel test execution by going to Setup|Develop|Apex Test Exectution|Options…|Disable Parallel Apex Testing. Finally you could refactor the code to have both the test methods in the same class.

  • Code coverage calculation

Code coverage percentage is calculated whenever tests are executed. The general formula for code coverage percentage calculation is:

Code coverage percentage = Number of covered lines / (Number of covered lines + Number of uncovered lines)

Tests are run in two situations, first is when you explicitly run them through the Developer Console or from the Setup menu, and second, implicitly when you are trying to deploy your code to another org. The code coverage percentage is calculated in both these cases but they are differently treated. In the first case the code coverage percentage is persisted to a code coverage table for the benefit of the Testing tools. In the second case it isn’t stored anywhere. The reason for this is that during deployment, apart from the apex code being deployed, there could also be some metadata being modified in the target org (for example, a lookup relationship being changed to Master-Detail). These metadata changes if unsuccessful (some child records not having parents while converting from lookup to M-D, point in case) could potentially rollback the deployment. Now, if the code coverage percentage was stored in this case and the rollback happened then the code coverage percentage could be considering some executable lines of the apex code which now don’t exist since they  were part of this deployment which has now rolled back.

In short, the code coverage percentage that is stored when you manually run tests isn’t referred to when deploying code, instead, the platform implicitly recalculates it on the fly.

Things that are not considered as a part of the code coverage percentage calculation include: Comments and blank lines, System.debug() statements and curly brackets when they appear alone on a line. Otherwise, multiple executable lines appearing on single line are calculated as one line. Single executable statement appearing on multiple lines is counted as multiple lines.

Some best practices that you should adhere to are as follows:

  • Test for good data, bad data and bulk data. Your test classes should have test methods for testing both the desired situation and undesired situations and they should check it for multiple records.
  • If there are multiple tests that are going to run in a single transaction, remember that all those tests would be consuming single set of governor limits. This could lead to some tests failing due to exhaustion of governor limits. Ensure that each test method gets its own fresh set of governor limits by making use of startTest() and stopTest() methods of the Test class.
  • To ensure users see only data that they are supposed to see when the apex code is executed in the background, you should test with Sharing. You can achieve this by using the System.runAs() method.
  • If you are using static resources in your tests, ensure that those resources are also available on the production org so that your tests don’t fail due to unavailability of the resource.
  • Don’t hardcode IDs in your tests, since IDs are unique across orgs.
  • Have only one test class per Apex class you are testing.

Of course there are numerous general testing best practices that also apply to apex tests. You should try following all of those. The above write-up does mention but a few that are specific to testing in Salesforce.

March 2, 2017/9,904 Comments/by Ashutosh
http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png 0 0 Ashutosh http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png Ashutosh2017-03-02 07:15:032017-03-02 07:15:03Testing Best Practices in Salesforce

Technical

Very basic understanding of SSL

The purpose of this post is to give a very high level non-technical overview of the transactions that happen when using SSL (Secure Sockets Layer) protocol. More details of the actual messages and its contents could be found on the internet…
20088 Comments
/
January 27, 2017
http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png 0 0 Ashutosh http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png Ashutosh2017-01-27 07:40:532017-02-10 15:02:41Very basic understanding of SSL
Salesforce.com, Technical

Understanding Outbound Messaging in Salesforce

Although most of the knowledge presented here has been taken from the Help & Training documentation provided by Salesforce.com, I have attempted to simplify it and put some important facts about Outbound messages in focus for the readers…
13797 Comments
/
January 27, 2017
http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png 0 0 Ashutosh http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png Ashutosh2017-01-27 07:30:452017-02-10 15:02:11Understanding Outbound Messaging in Salesforce
Salesforce.com, Technical

List views in Salesforce

Have you ever wanted a simple list of records for a marketing or a pre-sales activity without wanting to generate a report? Well List Views is your answer. List Views allow you to create criteria based lists of records, much like stored queries.…
11679 Comments
/
January 27, 2017
http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png 0 0 Ashutosh http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png Ashutosh2017-01-27 07:25:312017-02-10 15:02:25List views in Salesforce
Uncategorized

How to Web Check-in

Most of the Popular airlines provide a web check-in facility. As mentioned in my earlier post Web Check-in saved my day… , Web check-in saves you lots of trouble. In this post I intend to give some general directions of how to go about doing…
8893 Comments
/
January 27, 2017
http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png 0 0 Ashutosh http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png Ashutosh2017-01-27 07:10:082017-01-27 07:11:22How to Web Check-in
Technical

How to create a blog website

Well the title of this post does not exactly reflect what I am about to write. It should have rather read “How I created this Blog website” but the prior one is a more search friendly term. I am writing this post as a response to some frequently…
9883 Comments
/
January 27, 2017
http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png 0 0 Ashutosh http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png Ashutosh2017-01-27 07:04:482017-02-10 15:02:57How to create a blog website
Salesforce.com, Technical

External objects in Salseforce

Salesforce.com allows you to access data stored in your external data sources within your organization. Previously this was possible by writing your own adapters in Apex that used web services to access that data. This method of accessing the…
23880 Comments
/
January 27, 2017
http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png 0 0 Ashutosh http://www.cognitive.co.in/wp-content/uploads/2017/01/cognitive-official-logo.png Ashutosh2017-01-27 06:39:532017-02-10 15:03:11External objects in Salseforce
Page 1 of 3123

Thats me: Ashutosh Limaye

Perception|Learning|Reasoning

Categories

  • News (1)
  • Salesforce.com (4)
  • Technical (7)
  • Uncategorized (7)

Latest News

  • How to Web Check-inJanuary 27, 2017 - 7:10 am
  • Book Review: The Shiva Triology by AmishMarch 27, 2015 - 9:05 am
  • My review of Xiaomi MI4.February 12, 2015 - 5:12 am

Interesting links

Here are some interesting links for you! Enjoy your stay :)

Categories

  • News
  • Salesforce.com
  • Technical
  • Uncategorized

Archives

  • March 2017
  • January 2017
  • March 2015
  • February 2015
  • January 2015
  • August 2014

Sitemap

  • Home
  • About
  • Contact
© Copyright 2016. Cognitive. - powered by Enfold WordPress Theme
  • Facebook
  • Twitter
  • Gplus
Scroll to top