I have listed my code and test. I can't seem to cover this part: enter image description here

its at 68%. I have tried adding the two fields into the test class (time__c and Lag__c) but they are formula fields and the compilier wont take it. For the date field, i cant set it to just the year in the test class as well. not sure if any of that helps. Im still trying to learn how to put classes together and make sense of it all through googling and stackexchange but i cant come to a conclusion. any ideas?

  public with sharing class LCController {
   public class Data {
    public String loc {get; set;}

    public Decimal count {get; set;}

   public Decimal count2 {get; set;}

   @testVisible Data(String loc, Decimal count, Decimal count2) {
        this.loc = loc;

        this.count = count;
        this.count2= count2;


public Data[] getDatas() {
    Data[] datas = new Data[] {};
    for (AggregateResult ar : [
                    Location_Lookup__r.Name y,
                    AVG(Time__c) m,
                    AVG(Lag__c) c
            from App_c
            Where CALENDAR_YEAR(Date__c)= 2017 and Major__c ='WC' and   Location_Lookup__r.Parent_Name__c = 'TOPHIER' and    Type__c = 'TOP'
            group by Location_Lookup__r.Name
            order by Location_Lookup__r.Name

        datas.add(new Data(
               (String) ar.get('y'),
            (decimal) ar.get('m'),
             (decimal) ar.get('c')

    return datas;


public class LCtTest {

static testMethod void LCTest(){

 app__c b = new app__c();
b.Claim_Type__c = 'TOP';
b.Major__c ='WC';
 b.Location_lookup__c ='a1pj0000000EKZ4';
b.Date__c = Date.newInstance(2017, 12, 9);

    insert b;

    PageReference gp = Page.LCpage;

    LCController tcNew2Test = new LCController();

    LCController.Data myInnerClass = new LCController.Data('A', 2,3);

    system.assertEquals('A', myInnerClass.loc);
    system.assertEquals(2, myInnerClass.count);
    system.assertEquals(3, myInnerClass.count2);


2 Answers 2


Test methods do not see existing data in an org by default. So the object that has the Id 'a1pj0000000EKZ4' will not exist as far as the test is concerned: I'm surprised an error isn't generated when the app__c object is inserted in the test.

So the first thing to try is to insert the lookup object in the test and use the Id of the inserted object. The code for that would look something like this:

Location_Lookup__c l = new Location_Lookup__c(
        Parent_Name__c = 'TOPHIER'
insert l;
App__c a = new App__c(
        Claim_Type__c = 'TOP',
        Major__c ='WC',
        Location_lookup__c = l.Id,
        Date__c = Date.newInstance(2017, 12, 9)
insert a;

Then the query should return one row and give coverage of the line. Do also add an assert of the getDatas result: the main aim of tests is to confirm behaviour with test coverage just a metric that somewhat correlates.

(I've also changed the code to use the named constructor mechanism that can make test code a bit cleaner.)


One of the most basic best practices is to Avoid Hardcoding Ids. In using a hardcoded Id, you are relying on existing org data, which means your API Version must be 23 or earlier, else your test would fail. Salesforce is very consistent in recommending each unit test create its own data rather than using existing data in the org.

  • Isolation of Test Data from Organization Data in Unit Tests:

    Whenever possible, you should create test data for each test. You can disable this restriction by annotating your test class or test method with the IsTest(SeeAllData=true) annotation.

  • How to Write Good Unit Tests:

    Your unit tests should always create their own test data to execute against. That way, you can be confident that your tests aren’t dependent upon the state of a particular environment and will be repeatable even if they are executed in a different environment from which they were written.

  • Testing Best Practices:

    • Create the necessary data in test classes, so the tests do not have to rely on data in a particular organization.
  • Apex Hammer Test Results (emphasis mine):

    You can make a test run in this preferred manner by using the default behavior. Test methods only use org data when they are annotated with isTest(SeeAllData=true) or in a test class annotated with isTest(SeeAllData=true). Data silo tests are supported since API version 24.0. See Isolation of Test Data from Organization Data in Unit Tests.

    We highly recommend that as many of your tests as possible be data silo tests. The higher the percentage of data silo tests, the more effective the Hammer process is in finding potential issues in our code base. These issues could affect your org. This early detection enables Salesforce to identify and resolve bugs before we release new software.

You must log in to answer this question.

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