here I am trying to cover code for aggregate function. Here I am providing class code and test class, anyone have any clues why unable to cover.

public class ChartdataCon {

    public Integer allLeads{get;set;}
    public Integer TtlLeadNtQfy{get;set;}
    public Integer TtlQfyLeads{get;set;}  

    public ChartdataCon(){

    public void  leadDetails(){
        for( AggregateResult ar :[select count(id) total FROM Lead]){
            allLeads = integer.valueOf(ar.get('total'));
        for( AggregateResult ar : [select count(id) total,Status FROM Lead GROUP BY Status]){
            if(ar.get('Status') == 'Qualified'){
                TtlQfyLeads =  integer.valueOf(ar.get('total'));
            if(ar.get('Status') == 'Not Qualified'){
                TtlLeadNtQfy=  integer.valueOf(ar.get('total'));


public class ChartdataConTest {
    static void Testdata() {
        Lead ld = new Lead(
            Owner__c = 'Nadal',
            Status = 'Qualified'

        insert ld;
        Chartdatacon chart = new ChartdataCon();

  • Does that test compile and pass? Your ChartDataCon class does not have a count() method. – David Reed Aug 2 '18 at 12:48
  • Yes, but showing code coverage as 0%. – Laxman Aug 2 '18 at 12:50

Your method

static void Testdata() {

is not annotated with the @isTest or testMethod annotations. As such, it's not treated as a unit test and executed during test runs, and does not provide coverage. Add the annotation and rerun the test.

|improve this answer|||||
  • And one more thing I just want to add is that Aggregate Result functions don't need a separate test class. Able to get 100% code coverage now, Thank you!! David for correcting me. – Laxman Aug 2 '18 at 13:41
  • Aggregate queries do need to be covered, @Laxman. – David Reed Aug 2 '18 at 14:22

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.