1

I've been working to get one test to 100% coverage. I was having trouble with one statement. I do have data that tests both the null and not null branches.

Here's the statement I changed to get 100%.

ThingToAdd =  (SR_ID == null) ? 
    new ThingRequest__c() : [Select Name,First_Name__c,Last_Name__c,Start_Date__c,Thing_ID__c,Phone_Number__c 
    From ScholarRequest__c where ID = :SR_ID];

Note: the "from" line of the SOQL query was highlighted in red as not covered before I made my change.

What did I do to get code coverate to 100% ? I did not change the syntax of the SOQL query at all. I know the answer already, but I think it's interesting enough to merit sharing.

2
  • Your question is a little unclear here. If you're asking what you did to get code coverage to 100%, you'd want to share your code before and after? You'd also want to share your Apex class too so we could see what it is you'd want to cover.
    – Dan Jones
    Dec 8 '15 at 16:13
  • The class doesn't really matter since every other line had coverage. I will share the answer if someone else doesn't come up with the answer.
    – Jagular
    Dec 8 '15 at 16:23
3

Code coverage is glitchy at best. What you need to do is clear your test data, then re-run your unit test in asynchronous mode. If your unit test were actually testing both the null and non-null paths, you'd get 100% coverage. For example, I wrote this arbitrary code in my org:

public class TRA {
    public static id accountId;
    public static void doA() {
        Account a = accountId == null?
            new Account(): [SELECT Id, Name
                           FROM Account WHERE Id = :accountId];
    }
}

With the associated unit test:

@isTest class TRAT {
    // 75% coverage
    @isTest static void test1() {
        TRA.doA();
    }
    // 100% coverage
    @isTest static void test2() {
        Account a = new Account(Name='Test');
        insert a;
        TRA.doA();
        TRA.accountId = a.Id;
        TRA.doA();
    }
}

If you don't get those precise results for TRA, your coverage is glitched and needs to be reset.

For example, see this Known Issue.

You can reset your coverage statistics via Test > Clear Test Data in the Developer Console, and set the asynchronous flag by checking Test > Always Run Asynchronously.

1
  • Despite my prior confidence that all branches were covered, I was in fact missing one.
    – Jagular
    Dec 10 '15 at 20:16
5

Ok, based on sfdcfox's test I do not believe that this is what you did so will delete it when you post the answer...

You put it all on one line or moved the From to the second line.

possible bug in the code coverage as the From should be covered if the SOQL is executed

Code coverage has changed for statements that are written on multiple lines. Each line that contains an expression is now included in the code coverage calculation for covered and uncovered code. Previously, a multiline statement was calculated as one line of code in code coverage. This change affects all API versions.

Summer '15 Release Notes - New Code Coverage Calculation for Multiline Statements

2
  • @sfdcfox - OP stated he covered both conditions. So there must be a small bug in the code coverage where the From is not covered even though the SOQL is executed....
    – Eric
    Dec 9 '15 at 0:20
  • I just read that. Still, while that code coverage info is useful, that's probably the wrong solution...
    – sfdcfox
    Dec 9 '15 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.