0

How can I determine which lines are covered and which are not?

I have the following class that I need to test:

public class CasePageSelectorExtension 
{
    boolean isFieldAgent;
    String caseId;
    public CasePageSelectorExtension(ApexPages.StandardController controller) 
    {
        List<UserRole> roles = [SELECT Id FROM UserRole WHERE Name = 'FieldAgent'];
        isFieldAgent = !roles.isEmpty() && UserInfo.getUserRoleId() == roles[0].Id;
        caseId = controller.getRecord().id;
    }

    public Component.Apex.OutputPanel getCasePage() 
    {
        Component.Apex.OutputPanel panel = new Component.Apex.OutputPanel();
        if (isFieldAgent) 
        {

            Component.Apex.Detail detail = new Component.Apex.Detail();
            detail.subject = caseId;
            panel.childComponents.add(detail);
        }
        else 
        {
            Component.Support.CaseFeed caseFeed = new Component.Support.CaseFeed();
            caseFeed.caseId = caseId;
            panel.childComponents.add(caseFeed);
        }
        return panel;
    }
}

The following Test Class I have written returns 28% code coverage. Not sure what I can do to determine which lines passed and which did not get coverage.

@istest
public class TestCasePageSelectorExtension {
    private static testMethod void testRun(){  
        Case c = new Case();
        c.Description = 'Ed';
        c.Module__c = 'New Agenda';
        insert c;

        test.startTest();
            PageReference pageRef = Page.tabbedcase;
            Test.setCurrentPage(pageref);
            pageRef.getparameters().put('id',c.id);

            ApexPages.StandardController sc = new ApexPages.StandardController(c);
            CasePageSelectorExtension controller = new CasePageSelectorExtension(sc);   
        test.stopTest();
    }
}
1
  • 2
    I think this should be reopened as the answer to the question "Not sure what I can do to determine which lines passed and which did not get coverage" is pretty straightforward.
    – Eric
    Oct 25, 2016 at 3:27

1 Answer 1

1

To discover which lines are covered and which are not, you can use the Developer Console as outlined below:

Checking Code Coverage

The Developer Console retrieves and displays code coverage information from your organization. Code coverage results come from any tests you’ve run from an API or from a user interface (for example, the Developer Console, the Force.com IDE, or the Apex Test Execution page). To clear the current results, click Test | Clear Test Data. When you edit a class, the code coverage for that class is cleared until you run the tests again.

You can view code coverage in several places in the Developer Console.

  • The Tests tab includes an Overall Code Coverage panel that displays the code coverage percentage for every Apex class in your organization that has been included in a test run. It also displays the overall percentage.
  • Double-click a completed test run to open a Tests Results view that displays the tested class, the tested method, the duration, result (skip, pass, or fail), and an optional error message. If the test failed, a Stack Trace column shows the method and line number at which the test failed.
  • To view line-by-line code coverage for an Apex class, open the class. The Code Coverage menu will include one or more of the following options depending on the tests you have implemented:
    • None
    • All Tests: The percentage of code coverage from all test runs.
    • className.methodName: The percentage of code coverage from a method executed during a test run.

Lines of code that are covered by tests are blue. Lines of code that aren’t covered are red. Lines of code that don’t require coverage (for example, curly brackets, comments, and System.debug calls) are left white.

Code coverage in the Developer Console


You should note that what you have shown in your question is not a Unit Test. It is a Smoke Test. You should read How to Write Good Unit Tests start to finish, but especially this section (emphasis mine):

Verify the results are correct

Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests.

A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

You must log in to answer this question.

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