[SalesForce] Apex Code Coverage: Test classes are included with 0% in overall code coverage calculation

We are making heavy use of stub and mock objects in our Apex tests. Most of the time we are defining the mocks as inner classes of our test classes. We have noticed, that test classes, which do contain such mock classes do show up at the Overall Code Coverage in the developer console. Such a test class does always have a code coverage of 0% and 0/zz lines covered.

I give a short example. Consider the following class of the business logic:

public virtual class Foo {

    public virtual String doSomething() {
        return anotherFunction('foo');
    }

    public virtual String anotherFunction(String value) {
        return value;
    }
}

A typical unit test would look like the following:

@isTest
private class FooTest {

    private class FooMock extends Foo {
        private Boolean anotherFunctionCalled;
        private String value;

        public override String anotherFunction(String value) {
            this.anotherFunctionCalled = true;
            this.value = value;

            return 'test';
        }
    }

    @isTest
    static void testDoSomething() {
        FooMock f = new FooMock();

        String result = f.doSomething();

        System.assertEquals('test', result);
        System.assertEquals(true, f.anotherFunctionCalled);
        System.assertEquals('foo', f.value);
    }

    @isTest
    static void testAnotherFunction() {
        Foo f = new Foo();

        String result = f.anotherFunction('blah');

        System.assertEquals('blah', result);   
    }
}

The Overall Code Coverage shows the following:

code coverage

Foo: 100% 4/4 Lines covered
FooTest: 0% 0/2 Lines covered

If you click on Apex Classes/Estimate your organization's code coverage you will get the surprising result of 66.67% (4/(2+4)) code coverage, which will not allow you to upload this code as a package.

How can we make sure, that the test classes will not be considered at the coverage estimation?

Best Answer

It sounds like you may have a specific problem related to mocking; however, for those experiencing the general problem stated in the question title, "Test classes are included with 0% in overall code coverage calculation", there is another cause for this issue. As @Bujji stated in his answer, it does occur (as of Spring 15) that changing a class from a non-test class to a test class can leave coverage data behind in the org that does not appear to be cleaned up by running all tests or by clearing all test data. I have just experience this issue; in a code base that I inherited, a test utility (factory) class was not marked @isTest, and so was generating coverage - incomplete coverage, in fact. I marked it @isTest, but the class didn't get removed from coverage reporting - it just dropped to 0%, lowering my overall coverage.

Instead of deleting and re-creating the class, you can clean up the old coverage data. The documentation for this known issue (resolved in Spring 15) describes a method for clearing old coverage data which resolves this issue:

1) [experience issue]

2) In the Developer console run this Tooling API query

SELECT Id, ApexClassOrTriggerId, ApexClassOrTrigger.Name,
       NumLinesCovered, NumLinesUncovered 
  FROM ApexCodeCoverageAggregate 

3) Select all results and hit delete

4) Run all tests

If you haven't seen it before, the Query tab of the Developer console has a little "use tooling api" checkbox you'll need to check.

Related Topic