Apex code coverage hack
A hack to rescue in failed deployments due to code coverage
Disclaimer: The method mentioned below is a bad practice and should not be followed during development. It can be used in case of urgent deployment where test coverage falls to 74% and deployment needs to be done immediately.
Salesforce count the total number of lines in org and number of lines covered by test classes to calculate the coverage percentage and it can be tricked easily by writing few number of lines and covering those lines in test class.
Below are the classes for you to copy to increase the number of lines as well as covered lines
Class-
/*Class Name: IncreaseCoverage
Please remove this class as soon as you fix the coverage
*/
public class IncreaseCoverage{
public static void hack() {
Integer i = 0;
i++;
i++;
i++;
i++;
i++;
i++;
// you can continue this method with i++; up to 3000 lines
// after that you would be stopped by size limit
// but you can create a few such methods
}
public static void hack1() {
// do the same thing as above ...
}
//...
public static void hackN() {
// do the same thing as above ...
}
}
Test class-
//Test class
@isTest static void runTest() {
IncreaseCoverage.hack();
IncreaseCoverage.hack1();
//...
IncreaseCoverage.hackN();
}
That's all, now you can deploy your code to production and then think about increasing the coverage in the next release :)(don't forget to remove this code in next deployment).
Note: It is also not a good practice to keep the coverage at Edge(75%), it can be a big show stopper during production deployments. Better keep the coverage to at least 80% to be on a safer side during deployments.