0

I'm confused how Salesforce calculates test coverage. I've been struggling to get my app up to the 75% threshold. I have a number of files where I assume coverage should be 100%, but Salesforce says I only have 51% coverage. For instance, here is a class that only has one function, and I test this function, so I would expect to get 100% coverage. But Salesforce says I only have 51% coverage on this file. What do I need to do to get this coverage higher?

I'll shorten this file slightly to make it readable, but I have a class that has just one static function, and this is what it looks like:

/*
 *
 * type_of_permission should be either "select" or "insert" or "update", so you would call
 * using one of these 3 arguments:
 *
 *  SameDayPermissionModel.checkPermissions('select')
 *
 *  SameDayPermissionModel.checkPermissions('insert')
 *
 *  SameDayPermissionModel.checkPermissions('update')
 *
 */
public static boolean checkPermissions(String type_of_permission) {

    boolean permitted = true;


    if ( type_of_permission != 'select'  && type_of_permission != 'insert'  && type_of_permission != 'update'  && type_of_permission != 'delete' ) {
        return false; 
    }



    if (type_of_permission == 'select') {

        if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isAccessible()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDaySession__c.isAccessible()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__Search__c.isAccessible()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDayNote__c.isAccessible()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDayImport__c.isAccessible()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDay_Flash_Message__c.isAccessible()) {
            permitted = false;
        }

    }



    if (type_of_permission == 'insert') {

        if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isCreateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDaySession__c.isCreateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__Search__c.isCreateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDayNote__c.isCreateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDayImport__c.isCreateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDay_Flash_Message__c.isCreateable()) {
            permitted = false;
        }
    }




    if (type_of_permission == 'update') {

        if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isUpdateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDaySession__c.isUpdateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__Search__c.isUpdateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDayNote__c.isUpdateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDayImport__c.isUpdateable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDay_Flash_Message__c.isUpdateable()) {
            permitted = false;
        }
    }




    /*
     * 2017-08-09 -- I'm fairly sure that the only place we delete anything is in the unit tests,
     * where we create a test object, insert it, then delete it.
     *
     *
     */
    if (type_of_permission == 'delete') {

        if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isDeletable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDaySession__c.isDeletable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__Search__c.isDeletable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDayNote__c.isDeletable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDayImport__c.isDeletable()) {
            permitted = false;
        }

        if (!Schema.sObjectType.FastCash__SameDay_Flash_Message__c.isDeletable()) {
            permitted = false;
        }

    }

    return permitted;
}

And then I have these tests in place:

@isTest
static  void testCheckPermissionsUpdate(){

    // Perform test logic
    Test.startTest();

    boolean wasThisAllowed = SameDayPermissionModel.checkPermissions('update');

    Test.stopTest();

    System.assertEquals(true, wasThisAllowed, 'SameDayPermissionModel.checkPermissions(select) should return true -- if this is not true, then that means the admin who is installing this app has restricted the permissions to a point that the SameDay Search Tool will not run correctly, in which case it is appropriate that this test should fail.');

}


@isTest
static  void testCheckPermissionsIncorrect(){

    // Perform test logic
    Test.startTest();

    boolean wasThisAllowed = SameDayPermissionModel.checkPermissions('an incorrect string');

    Test.stopTest();

    System.assertEquals(false, wasThisAllowed, 'SameDayPermissionModel.checkPermissions() takes either select, insert, or update as a string and then tests either isAccessbile() or isCreateable() or isUpdateable(). If some other string, besides those 3, is given as an argument, then the return value is false.');

}

I have tests for all four allowed inputs, but I cut those for the sake of brevity.

All tests are passing.

Can anyone tell me why Salesforce does not consider this 100% coverage? Why is this only 51% coverage? What else do I need to do to get 100%?

[ [ UPDATE ] ]

Robert Watson pointed me to the coverage report. I now see that Salesforce does not feel that these lines are tested:

        if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isAccessible()) {
            permitted = false;
        }

But as near as I can see, these lines are tested, they are testing to see if a custom object has a permission.

1 Answer 1

3

It's no surprise at all you don't get 100% coverage. You need to test each code path. Here, you have variations on:

if (input == 'select')
{
    // Code Block 1
}
if (input == 'insert')
{
    // Code Block 2
}
if (input == 'update')
{
    // Code Block 3
}
if (input == 'delete')
{
    // Code Block 4
}

But you only check Code Block 3. You need to create a separate unit test each where you pass select, insert, and delete.

6
  • Sorry, but I cut some code, in the above examples, for the sake of brevity. In my actual production code, I do test all 5 possibilities, including the 4 allowed arguments, plus the possibility of incorrect arguments. But I only get credit for 51% unit test coverage, which does not make sense to me.
    – LRK9
    Commented Aug 15, 2017 at 17:00
  • Well your question is not really written in a way that makes answering possible. We can't tell which lines are covered or not. I recommend breaking it out into multiple smaller methods which are easier to test.
    – Adrian Larson
    Commented Aug 15, 2017 at 17:02
  • 1
    @LRK9 If you open the file in the Developer Console, you should see a button on the left for Code Coverage. It will show all test methods that call this class, and clicking through will highlight the lines that are covered and not covered. That should help you figure out what's not being called. Commented Aug 15, 2017 at 17:26
  • Robert Watson, thank you for that. Now that I look at this in the Developer console coverage report, I am surprised to see that these fields are not considered tested: if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isAccessible()) { permitted = false; }
    – LRK9
    Commented Aug 15, 2017 at 18:22
  • What could I do to make Salesforce see those lines as tested? It seems to me they are tested, they check to see if a given custom object has a particular permission set.
    – LRK9
    Commented Aug 15, 2017 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .