+ Start a Discussion
satish marisetty 10satish marisetty 10 

how can i cover exception of this method

how can i conver exception catch block of this method in my test class

public static Boolean checkFaultAnalysis(String stockingRecordId,String mmId){
        Boolean faultAnalysisFlag = false;
        try{
            if(!String.IsEmpty(stockingRecordId) && !String.isEmpty(mmId)){
                
                List<ICS_WR_Customer_Warranty_Config_Matrix__c> faultMatrix = [SELECT id,Name,RecordTypeId FROM ICS_WR_Customer_Warranty_Config_Matrix__c WHERE ICS_WR_Stocking__c=:stockingRecordId AND ICS_WR_MMID__r.Name=:mmId AND RecordTypeId=:Label.ICS_WR_Fault_Analysis_RT_ID AND ICS_WR_Inactive__c = false];
                System.debug('faultMatrix ='+faultMatrix );
                if(faultMatrix!=null && faultMatrix.size()>0)
                    faultAnalysisFlag = true;
            }
        }catch(Exception e){
            System.Debug(e.getMessage());
            ApexPages.addMessages(e);
            String strInvocationID ='ICS_WR_Warranty_CreateExtension'+UserInfo.getUserId() + '_' + system.now().formatGmt('yyyyMMddkkmmssSS')
                + '_' + String.valueOf((Math.random() * 10).round()).leftpad(3,'0');
            Core_Log_Entry.logEntryWithException(strInvocationID,System.Label.ICS_WR_logging_Scope,'ICS_WR_Warranty_CreateExtension__checkFaultAnalysis','Error',e.getMessage(),'',e);
        }
        return faultAnalysisFlag;
    }
Lalit Mistry 21Lalit Mistry 21
Hi Satish,

Assuming the above method is defined in a class named FaultAnalyzer, update your class as below
public with sharing class FaultAnalyzer {
	@TestVisible
	private static Boolean testException = false;
	
	public static Boolean checkFaultAnalysis(String stockingRecordId,String mmId){
		Boolean faultAnalysisFlag = false;
		try{			
			if(!String.IsEmpty(stockingRecordId) && !String.isEmpty(mmId)){
				
				List<ICS_WR_Customer_Warranty_Config_Matrix__c> faultMatrix = [SELECT id,Name,RecordTypeId FROM ICS_WR_Customer_Warranty_Config_Matrix__c WHERE ICS_WR_Stocking__c=:stockingRecordId AND ICS_WR_MMID__r.Name=:mmId AND RecordTypeId=:Label.ICS_WR_Fault_Analysis_RT_ID AND ICS_WR_Inactive__c = false];
				System.debug('faultMatrix ='+faultMatrix );
				if(testException)
					faultMatrix = null;
				if(faultMatrix.size()>0)
					faultAnalysisFlag = true;
			}
		}catch(Exception e){
			System.Debug(e.getMessage());
			ApexPages.addMessages(e);
			String strInvocationID ='ICS_WR_Warranty_CreateExtension'+UserInfo.getUserId() + '_' + system.now().formatGmt('yyyyMMddkkmmssSS')
				+ '_' + String.valueOf((Math.random() * 10).round()).leftpad(3,'0');
			Core_Log_Entry.logEntryWithException(strInvocationID,System.Label.ICS_WR_logging_Scope,'ICS_WR_Warranty_CreateExtension__checkFaultAnalysis','Error',e.getMessage(),'',e);
		}
		return faultAnalysisFlag;
	}
}

And then below test method should cover for the exception block
private with sharing class FaultAnalyzerTest {
	
	private static testMethod void testForException(){
		FaultAnalyzer.testException = true;
		System.Test.startTest();
		FaultAnalyzer.checkFaultAnalysis(null, null);
		System.Test.stopTest();
	}
}

Hope this helps.
satish marisetty 10satish marisetty 10
Thanks
Lalit Mistry 
 
Lalit Mistry 21Lalit Mistry 21
Mark this as an answer if it works, for benefit of others.