0

Below is the code of negative scenario: public void onAddExistingGuest(){ selectedGroups = new Set<Grouping__c>();

    ApexPages.Message msg;
    msg = new ApexPages.Message(ApexPages.Severity.Error, System.Label.Value_Here);
        ApexPages.addMessage(msg);
    
    try{
        Kids_Club_Requests__c Kidsclubreq= getKidsclubreq();
        Boolean tempVal = false;
        
        if(Kidsclubreq != null ){    
             
           for( Grouping__c objgrouping : [select id,Name from Grouping__c  where id = :Kidsclubreq.Tour_Grouping_Name__c limit 1]){                
                mapOfGrouping.put(objgrouping.id,objgrouping);}
        lstgrouping.clear();
         lstgrouping.addAll(mapOfGrouping.values());
         displayaddexistingPopUp = false; 
              
                for(Grouping__c lstgroupingVar : mapOfGrouping.values()){
                    strToHoldGrpIds += lstgroupingVar.id + ConstantsClass.COMMA_VALUE;
                }
           
        }else{
            **msg = new ApexPages.Message(ApexPages.Severity.Error, ConstantsClass.KIDS_CLUB_ERROR_MESSAGE);
            ApexPages.addMessage(msg);
            errorMessageToShow = ConstantsClass.KIDS_CLUB_ERROR_MESSAGE;**
        }**strong text**
     
    }catch (Exception excep){
        //ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, 'Error!'+excep.getMessage());
        msg = new ApexPages.Message(ApexPages.Severity.Error, System.Label.Error_Group_Add);
        ApexPages.addMessage(msg);
        errorMessageToShow = System.Label.Error_Group_Add;
    }
}

I need to cover the code coverage of negative scenarios like above: msg = new ApexPages.Message(ApexPages.Severity.Error, System.Label.Error_Group_Add); ApexPages.addMessage(msg); errorMessageToShow = System.Label.Error_Group_Add;

4

1 回答 1

1

准备数据,以便在触发错误时为您提供案例并catch捕获该错误。唯一可以给出错误的部分 -getKidsclubreq方法/只有这个可以抛出异常。
如果您无法提出可能产生异常的案例 -try...catch完全删除块。有些情况下您无法测试catch块,但这不是其中之一。

于 2022-01-24T04:10:11.707 回答