1

Below is a Visualforce Chart Controller example directly from the offical documentation.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_charting_overview_simple_example.htm

public class PieChartController {
public List<PieWedgeData> getPieData() {
    List<PieWedgeData> data = new List<PieWedgeData>();
    data.add(new PieWedgeData('Jan', 30));
    data.add(new PieWedgeData('Feb', 15));
    data.add(new PieWedgeData('Mar', 10));
    data.add(new PieWedgeData('Apr', 20));
    data.add(new PieWedgeData('May', 20));
    data.add(new PieWedgeData('Jun', 5));
    return data;
}

// Wrapper class
public class PieWedgeData {

    public String name { get; set; }
    public Integer data { get; set; }

    public PieWedgeData(String name, Integer data) {
        this.name = name;
        this.data = data;
    }
}
}

How exactly would you write a test class for this. How do I add data to the chart specifically?

5

Your test would just instantiate the class and request the list:

@isTest static void test() {
    PieChartController ctrl = new PieChartController();
    PieChartController.PieWedgeData[] data = ctrl.getPieData();
    // Verify data contains correct values, via System.assert
}

This example uses hard-coded data, so there's nothing to add. In the "real world", your getPieData() function would call a query, then generate data from that. Accordingly, if you were using a controller that used such a query, you'd just populate those records in the database via normal insert commands:

SObject[] records = new SObject[0];
// populate records
// Afterwards...
insert records;
Test.startTest();
PieChartController ctrl = new PieChartController();
PieChartController.PieWedgeData[] data = ctrl.getPieData();
Test.stopTest();
// Verify data contains correct values, via System.assert

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.