Tuesday, February 6, 2018

[Salesforce / Apex Test] Code coverage and logical tests in Apex Test Classes

This article is actually a repost from a buddy I came across online.

I read his articles on LinkedIn by chance and I really appreciated the easy way he writes and how he delivers important Salesforce concepts.

I choose an article about Apex Test Coverage... but before going on the article...
...please take a moment to hail our beloved lord the deployment fish!


The guest blogger

Vladimir Egikyan is a SFDC developer since 2010. Most of his career he worked for technical services providing company in Israel. During that time he helped to implement business of various companies, among which are ZIM Integrated Shipping Services, Bank Leumi.
In the latest three years he works as in-house developer and sharpen his architect skills. Though he is officially certified only as Developer... yet ;)



Writing good Apex Tests is a key to successful developing. There is a huge list of best practices and recommendations, that you can find in Salesforce documentation. Following them all, is what every developer should aim to do. Over years of developing on Salesforce, I learnt that code coverage is not what really matters…

How can that be?

Well, your coverage will raise by itself if you mind checking what your class or trigger has to do and what results you expect. Having said that, the most important thing that your test class has to do is to use System.assert methods to prove that code behaves properly. This will keep you on the safe side from any unintentional mistakes, changes in business requirements and eventually will save time and money on development process.

Okay, let’s take a look at the example:


TVRemoteControl class describes remote controls that can increase and decrease volume in scale 0-50 and have simple menu text.

Corresponding test class would contain call for each of class’ function:


TVRemoteControlTest covers the TVRemoteControl by 86% which is more than allowed minimum of 75% and these classes will deploy to production.

First and obvious defect of the test class is that it does not test if code will properly handle changing the volume beyond limits. The most important is we have to test that we get expected results in any possible use case. Our applications are almost never finished: business requirements constantly change. Besides that, we are all human and we always can make mistakes and test class is a tool that can help us to correct possible mistakes in time and that happens quite a lot.

Let’s see the better version of the test class.


This version of test class contains checking for going out of boundaries and also in each test method implements different use case and uses System.assert methods to make sure that class is performing the expected logic.

The given example is simple, but do not underestimate the importance of using assertion methods. Business logic in real life is way more complex then tv remote control, keep using these methods and you will be surprised how much time it saves for future technical support.

5 comments:

  1. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites! Stahl’s Essential

    ReplyDelete
  2. It’s amazing to visit again n again coming to your blogs the superb effort is here.
    belt dress

    ReplyDelete
  3. Salesforce Service Cloud is a game-changer for businesses looking to elevate their customer service experiences. By streamlining case management, offering omnichannel support, providing self-service options, leveraging AI-powered insights, and promoting team collaboration

    ReplyDelete
  4. The underground and the basement both refer to spaces below ground level, they serve distinct purposes and have different characteristics. The underground primarily accommodates infrastructure and transportation networks, while the basement is designed as a usable living or storage space within a building

    ReplyDelete
  5. The roof's construction is crucial for defining the character of your house as a home, just like the memories we make. Top Roofing Companies .In spite of the fact that your house is meant to be a haven from the outside world, it doesn't feel like much of one when entering doesn't keep the rain off of you. One call to our roofing business can return your comfort and sense of security. Your roofing issues can be resolved by our expert labor team, who has years of industry knowledge and offers top-notch customer service.


    ReplyDelete