Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Bypass Unit Test Code Coverage - Salesforce developer tutorial
Apex

Bypass Unit Test Code Coverage

The short answer

A temporary workaround for pushing Apex test code coverage above the 75% deployment threshold by adding and invoking dummy code. It shows how executed filler lines change the ratio of covered lines to total lines, and the post is clear that this is a bad coding practice.

Key takeaways Salesforce requires at least 75% unit test code coverage to deploy Apex classes to production. Apex code coverage is calculated by dividing the number of executed lines by the total lines of code in the class and multiplying by 100. Adding a dummy static method with multiple executable lines and calling it from a test class can artificially increase the covered line percentage for urgent deployments. Avoid using artificial line-padding methods for code coverage during normal development because it is considered a bad coding practice.

Hopefully this is old news, but atleast 75% code coverage by unit test is needed in order to deploy apex classes to production. Sometimes we have a situation (I had it many times!!) where a class needs to be deployed but coverage is hitting around 70% - 72%. You don't have time and client is just sitting on your head, What to do now? You are exhausted and having no idea what is failing, how to fix it.

Here I have a small trick using which you can get rid of this situation (for time being) and deploy the class in production urgently.

Before moving further, you should understand how the coverage is calculated. Lets say you are having an apex class of 100 lines. With the test class, 40 lines are getting covered, so the total coverage of the class is 40/100 * 100 = 40%.

For example, we have following apex class

public class A{
  public void method1(){
    try{
         //100 lines of code
     }
     catch(exception e){
        system.debug(e.getMessage());
     }
  }
}

You have written test class for it, but out from 100 lines under try block, the code is failing at line 40 and its being captured under catch block. So overall code coverage is going about 40 - 42% (lets assume). You are not sure what is causing this issue but need to be deployed as soon as possible.

Trick is, you need to create another method in your apex class. Which will look like as follows

public class A{
  public void method1(){
    try{
         //100 lines of code
     }
     catch(exception e){
        system.debug(e.getMessage());
     }
  }
  public static void fakeMethod(){
      integer i = 0;
      i++;
      i++;
      i++;
      i++;
      //repeat i++ statement each line for 195 times
  }
}

Here we added one fakeMethod having 200 lines of code (As per Salesforce) but its meaningless (I know but this is the trick). Compiler will compile the code is having app 100 + 200 = 300 lines. Now in test class you need to invoke just a method like below:

@isTest
class global TestClass{
  static testMethod void fakeTest(){
     //whatever logic you have for test method earlier
     A.fakeMethod(); //method invocation
  }  
}

So here, with this trick, total lines covered are 40 + 200 = 240. The coverage for the apex class will be now (240/300 * 100) = 80%.

Voila, Thats it! This is also known as "The power of 1". I would not recommend to use this trick as its ofcourse a bad coding practice, but better you should know.

Frequently asked questions

What is the minimum code coverage required to deploy Apex to production?

At least 75% unit test code coverage is required to deploy Apex classes to a Salesforce production environment.

How is Apex code coverage calculated?

Code coverage is calculated by dividing the number of lines covered by test methods by the total number of lines in the Apex class and multiplying the result by 100.

How do you artificially increase Apex test code coverage?

You can add a static method containing multiple simple statements, such as repeated variable increments, to the Apex class and call that method from a test class. While this increases the percentage of covered lines, it is a bad coding practice and not recommended for standard development.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment