Inflate Your Test Coverage

Disclaimer: I do not recommend doing this in 99% of cases. It is a “hack” to circumvent the best practice that really is at the core of Apex development and deployment for Salesforce.

Do you hate the requirement that all Apex classes must have 75% test coverage before being able to be deployed or packaged? Since test coverage detector is not the most intelligent piece of software in the universe, it’s fairly easy to inflate this number.

The strategy? Simple: add a bunch of garbage lines to your Apex class that really bear no weight against its typical operation, but are consulted when the test coverage is being evaluated. For example:

public class MyClass {
  public void myMethods() {
    //...
  }
  public void inflateTest() {
    Integer j = 0;
    j++;
    j++;
    j++;
    j++;
    j++;
    j++;
    // you get the idea
  }
  static testMethod void runTest() {
    MyClass c = new MyClass();
    c.inflateTest();
  }
}

You can put as much in inflateTest() as you want (within governor limits, of course), and this will only be evaluated at Test time. Code Coverage: 99%.

Note that the method that contains my business logic (myMethods()) is not actually being tested and we have a sufficiently high test coverage %.

This entry was tagged , , . Bookmark the permalink.

5 Responses to Inflate Your Test Coverage

  1. Taggart Matthiesen says:

    While I would love to write this post off as an April Fool’s joke, I think it might be better to take this opportunity to highlight that we are “fixing” our logic to flag this type of anti-pattern. With this being said, the team is also focused on extending the framework functionality (mock objects, smart deploy, and siloed data) which will provide even greater testing support.

    Feedback is always welcome.

  2. Ray Dehler says:

    Good news!

  3. David Rennert says:

    Looks like you need to put the each dummy statement in a new line.
    This worked for me:
    j++;
    j++;
    j++;
    .
    .
    but this didn’t:
    j++; j++; j++;…

    • Ray Dehler says:

      @David Rennert: Right, that makes sense. We’re talking about # of lines covered, not any other metric.

  4. Ramesh says:

    This is very wonderful new for me.
    I written some Dummy code,It is working for me.

    Thank Q very much for Sales force professional /Sales force Developers

Comments are closed.