• Shilpa Goyal
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
i am having 90% code coverage because atch block is not covered,can anyone help in writing code for catch block as well

Apex code -->
public class PickListHandler {
    @AuraEnabled
    public static List<String> getLevel1(){
    List<String> tempLst1 = new List<String>();
        for(AggregateResult  ar : [select Level_1__c,COUNT(id) from Case_Type_Data__c  group by Level_1__c])
    {
        tempLst1.add(''+ar.get('Level_1__c'));
    }

    return tempLst1;
      
      
    } 
    
    @AuraEnabled
    public static List<String> getLevel2(string strName){
    List<String> tempLst2 = new List<String>();
       for(AggregateResult  ar : [select Level_2__c,COUNT(id) from Case_Type_Data__c where Level_1__c=:strName  group by Level_2__c])
    {
       tempLst2.add(''+ar.get('Level_2__c'));
    }

    return tempLst2;
      
    } 
    
    @AuraEnabled
    public static List<String> getLevel3(string strName1,string strName2){
     List<String> tempLst3 = new List<String>();
      for(AggregateResult  ar : [select Level_3__c,COUNT(id) from Case_Type_Data__c  where Level_1__c=:strName1 and Level_2__c=:strName2 group by Level_3__c])
    {
       tempLst3.add(''+ar.get('Level_3__c'));
    }

    return tempLst3;
      
      
    } 
         
     @AuraEnabled
     Public  static String  savecasetype(string level1,string level2,string level3,string caseid){
     string strMsg='successfull';
          try{
     ERT_Case_Type__c obj=new ERT_Case_Type__c();
     Obj.Case__c =caseid;
     Obj.Level_1__c=level1;
     Obj.Level_2__c=level2;
     Obj.Level_3__c=level3;
     Insert obj;
     Return 'Successfully Inserted Levels';
     }
     
    catch(Exception ex){
            strMsg='error';
        }
     return strMsg;  
}

}

Test class code -->
@isTest
public class testGetAllLevels { 

static testMethod void testGetLevel1()
{
    Case_Type_Data__c obj = new Case_Type_Data__c();
    obj.Level_1__c = 'Test Level 1';
    insert obj;
    List<String> s = PickListHandler.getLevel1();

}
    
static testMethod void testGetLevel2()
{
    Case_Type_Data__c obj = new Case_Type_Data__c();
    obj.Level_1__c = 'Test Level 1';
    insert obj;
    List<String> s = PickListHandler.getLevel2('Test Level 1');

}
    
static testMethod void testGetLevel3()
{
    Case_Type_Data__c obj = new Case_Type_Data__c();
    obj.Level_1__c = 'Test Level 1';
    obj.Level_2__c = 'Test Level 2';
    obj.Level_3__c = 'Test Level 3';
    insert obj;
    List<String> s = PickListHandler.getLevel3('Test Level 1','Test Level 2');

}
    
    
static testMethod void testsaveCaseType(){
        // Create the Case Record.
        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
        insert cas;
       
        ERT_Case_Type__c obj=new ERT_Case_Type__c();
        string one='one';
        string two='two';
        string three='three';
        test.startTest();
        String testing=PickListHandler.savecasetype(one,two,three,cas.id);
        test.stopTest();
    }
    
 
    
}

Thanks in advance
Carolyn