+ Start a Discussion
Bryan TelfordBryan Telford 

Trying to get covered for a VF calendar project

I am trying to get code coverage for the following controller. This is based on the calendar project found at https://github.com/sxg133/VFCalendar

*   Controller for calendar component demo
*   @author Sahil Grover 
public with sharing class CalendarController implements CalendarItemRetriever {

    public CalendarHandler Cal {get; private set;}
    public CalendarParameters CalParams {get; private set;}

    public CalendarController() {
        CalParams = new CalendarParameters();
        CalParams.SelectedView = CalendarParameters.CalendarView.MONTH;
        Cal = new CalendarHandler(this);

    *   Get calendar items in a date range
    *   @param  startDate   The start date (inclusive) to retrieve calendar items for
    *   @param  endDate     The end date (inclusive) to retrieve calendar items for
    *   @return A list of CalendarItem objects
    public List<CalendarItem> getCalendarItems(Date startDate, Date endDate) {
        List<CalendarItem> calendarItems = new List<CalendarItem>();
        // custom events
        List<Calendar_Event__c> customEvents = new List<Calendar_Event__c>([
            SELECT Id, Name, Description__c, Start_Date_Time__c,
                End_Date_Time__c, Is_All_Day__c, Link__c, Trainer__c
            FROM Calendar_Event__c
            WHERE Start_Date_Time__c >= :startDate
                AND End_Date_Time__c <= :endDate
        for (Calendar_Event__c e : customEvents) {
            CalendarItem calItem = createCalendarItem(e);

        return calendarItems;

    private CalendarItem createCalendarItem(Calendar_Event__c e) {
        CalendarItem calItem = new CalendarItem(e.Id);
        calItem.Name = e.Name;
        calItem.Description = e.Description__c;
        calItem.StartDateTime = e.Start_Date_Time__c;
        calItem.EndDateTime = e.End_Date_Time__c;
        calItem.IsAllDay = e.Is_All_Day__c;
        calItem.Link = e.Link__c;
        calItem.Trainer = e.Trainer__c;
        calItem.CssClass = 'custom-event';
        return calItem;

Test class
private class CalendarControllerTest{

      private static testMethod void CalendarTest() {
        CalendarController calendar = new CalendarController();


It gets covered 50% simply by calling CalendarController calendar = new CalendarController(); in the Test.startTest() method.

I know this is probably a somewhat simple apex test question, but I cannot figure out how to get the rest of the code covered.

Any help is appreciated!
Alain CabonAlain Cabon

For the questions about code coverage, always precise the uncovered lines very well given by the "developer console" that helps a lot for the support.


It is a custom controller.

Testing Custom Controllers and Controller Extensions: