You need to sign in to do that
Don't have an account?
Gleb 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:
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:
Any help is much appreciated.
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.
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.
All Answers
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.
Please, see simplified example below
APEX CLASS
RELATED TEST
As a result, 0 line of code are covered by test. What am I doing wrong?
Thanks in advance.
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.