1

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(){
        leadDetails();
    }

    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'));
            }
        }
    }
}

and my TEST CLASS

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

        insert ld;
        Test.startTest();
        Chartdatacon chart = new ChartdataCon();

        system.assertEquals(chart.count(),100);
        Test.stopTest();
    }
}
  • 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
3

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.