function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gleb VrevskyGleb Vrevsky 

Test class for class with only DB queries

Hello!

Hope you could help me with the developing test class - have tried several ideas, nothing actually works.
Initial task: output on visualforce page record A grouped by record B. 
To achieve wished result (result looks smth like ->  http://www.screencast.com/t/AocRaNSytH) custom controller was created.
Controller sample code:
public with sharing class DisplayGroupedCourses {

  public List<CourseEnrollment__c> courses {get;set;}
  public List<CourseEnrollment__c> courses_all {get;set;}
  public List<Employee__c> employees {get;set;}
  public Integer emplCount {get;set;}
  public Integer courseCount {get;set;}
  public String[] names {get;set;}

  public void load() {

    // Get courses with status completed
    courses = [SELECT Course__r.name, Course__r.id, Employee__r.name, Status__c, 
               DateExpectedOn__c, DateCompletedOn__c 
               FROM CourseEnrollment__c 
               WHERE Status__c = 'Completed'
               ORDER BY Employee__r.name ASC, DateCompletedOn__c DESC
              ];
    // Get all employees   
    employees = [SELECT Id, Name, Email__c, MobilePhone__c, Skype__c, 
                 PersonalEmail__c, EntryDate__c, Photo__c, EmployeeRole__r.name   
                 FROM Employee__c
                 WHERE Status__c != 'Disabled'
                 ORDER BY EntryDate__c ASC
                ];

    <!-- OTHER querie !-->

    // Dynamically create set of unique names from the query
    Set<String> nameSet = new Set<String>();
      for (CourseEnrollment__c c : courses) {
          nameSet.add(c.Employee__r.name);          
      }

    // Convert the names set into a string array  
    names = new String[nameSet.size()];
    Integer i = 0;
    for (String name : nameSet) { 
      names[i] = name;
      i++;
    }
  }
}

All works great, the only thing left - create a test class. Could someone give me ideas where to start or provide some sample code to cover class above with tests? I was trying to fill in created lists with sample data, but can't figure out what is the right way to do it.
I have started with:
 
@isTest
    public class DisplayGroupedCoursesTest { 
        static testMethod void myUnitTest() { 
            Employee__c e = new Employee__c();
            e.name = 'glebs vrevskis';
            insert e;
            //REFERENCE TO MY METHOD SHOULD GO HERE? 
            
            system.assertEquals('glebs vrevskis', e.name); 
        }
    }

 Any help is much appreciated.
Best Answer chosen by Gleb Vrevsky
James LoghryJames Loghry
Gleb, you'll need to construct your Apex class and then call the load method to actually test it.  See the code below for more info.  Also, as a best practice, you'll want to add assertions to assert that your class is performing as you'd expect.

Lastly, it looks like you've started inserting mock employee records, but you'll want to also include course mock records.  You should insert = records that both match the SOQL criteria (positive condition) and do not match the SOQL criteria (error condition) and assert that the lists in your controller are the expected size after you call the load method.
 
@isTest
    public class DisplayGroupedCoursesTest { 
        static testMethod void myUnitTest() { 
            Employee__c e = new Employee__c();
            e.name = 'glebs vrevskis';
            insert e;
            //REFERENCE TO MY METHOD SHOULD GO HERE? 
            DisplayGroupedCourses dgc = new DisplayGroupedCourses();
            dgc.load();

            System.assert(!dgc.courses.isEmpty());
            System.assert(!dgc.employees.isEmpty());

            system.assertEquals('glebs vrevskis', e.name); 
        }
    }

 

All Answers

James LoghryJames Loghry
Gleb, you'll need to construct your Apex class and then call the load method to actually test it.  See the code below for more info.  Also, as a best practice, you'll want to add assertions to assert that your class is performing as you'd expect.

Lastly, it looks like you've started inserting mock employee records, but you'll want to also include course mock records.  You should insert = records that both match the SOQL criteria (positive condition) and do not match the SOQL criteria (error condition) and assert that the lists in your controller are the expected size after you call the load method.
 
@isTest
    public class DisplayGroupedCoursesTest { 
        static testMethod void myUnitTest() { 
            Employee__c e = new Employee__c();
            e.name = 'glebs vrevskis';
            insert e;
            //REFERENCE TO MY METHOD SHOULD GO HERE? 
            DisplayGroupedCourses dgc = new DisplayGroupedCourses();
            dgc.load();

            System.assert(!dgc.courses.isEmpty());
            System.assert(!dgc.employees.isEmpty());

            system.assertEquals('glebs vrevskis', e.name); 
        }
    }

 
This was selected as the best answer
Gleb VrevskyGleb Vrevsky
@James, thank you for your prompt reply! You really helped, the general idea is clear, but I am trying to run simplified version of that code, but it is not counted as "test covered"

Please, see simplified example below

APEX CLASS
public with sharing class DisplayGroupedCourses {

  public List<Employee__c> employees {get;set;}

  public void load() {

    // Get all employees
    employees = [SELECT Name
                 FROM Employee__c
                ];
  }
}

RELATED TEST
@isTest
    public class DisplayGroupedCoursesTest { 
        @isTest static void myUnitTest() { 
            Employee__c e = new Employee__c();
            e.name = 'glebs vrevskis';            
            insert e;
            
            DisplayGroupedCourses dgc = new DisplayGroupedCourses();
            dgc.load();
            
            system.assertEquals('glebs vrevskis', e.name);

        }
    }

As a result, 0 line of code are covered by test. What am I doing wrong?

Thanks in advance.
James LoghryJames Loghry
Are you running this through the developer console?  If so, go to Test->Clear Test Data in the dev console, then re-run your unit test.

Otherwise, try going to Setup->Develop->Apex Test Execution, clicking on "View Test History", then clicking "Clear Test Data" an re running your unit test.
Gleb VrevskyGleb Vrevsky
Thanks a lot, it helped!