Search code examples
salesforceapexsalesforce-lightning

How do i write test class for below apex code


Please help me in writing test class for below apex code,i wrote a test class which shows only 66% coverage,i am looking for 100%


public class PickListHandler {
    @AuraEnabled
    public static List<String> getLevel1(string strName) {   
        List<String> tempLst = new List<String>
        for(AggregateResult  ar : [select Level_1__c,COUNT(id) from Case_Type_Data__c  group by Level_1__c])
        {
            tempLst.add('Level 1 data is'+ar.get('Level_1__c'));

            return tempLst;
        } 
    }   
}

Here is test class

@isTest
public class testGetLevel1 {
    static testMethod void testGetLevel1() {
        List<String> s = PickListHandler.getLevel1('test');
        //System.assert(....);
    }
}

Solution

  • You need need to create test data for the object Case_Type_Data__c. If you don't create data, the logic inside for loop will not execute.