+ Start a Discussion
Shiv ShankarShiv Shankar 

How to get test coverage for catch part

Hello friends, 

i have following class with test method i want to know how can i get test coverage for catch part. 

public Class AutoCompleteController{
    
    public String[] jsonDescriptionData;
    
    public AutoCompleteController(BrowseCatalog controller) {

    }
    
    public AutoCompleteController(){
    	
    }
    
    public List<string> getjsonDescriptionData(){
        try{
            if(jsonDescriptionData == null){
                jsonDescriptionData = new List<string>();
            }
            else{
                jsonDescriptionData.clear();
            }
            List<Product__c> productDesList = [select id, Name, Default_Sales_Description__c from product__c];
            if(productDesList != null && productDesList.size() != 0){
                Set<String > temp = new Set<String>();
                for(product__c rec : productDesList ){
                    temp.add(rec.Default_Sales_Description__c);
                    temp.add(rec.Name);
                }
                for(String rec : temp){
                    jsonDescriptionData.add('"'+rec+'"');
                }
            }
            return jsonDescriptionData;
        }catch(Exception e){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,e.getMessage()));
             return null;
        }
    }
    
    public static testMethod void testAutoCompleteController(){
       List<Product__c> proList = new List<Product__c>();
        Product__c pro1 = new Product__c(Name='111', Active__c=true, Alcohol__c=10, Allergen_OK__c='Yes', CAN_TTB_Approved__c='yes', Default_Sales_Description__c = 'Lemon Tea');
        Product__c pro2 = new Product__c(Name='112', Active__c=true, Alcohol__c=10, Allergen_OK__c='Yes', CAN_TTB_Approved__c='yes', Default_Sales_Description__c = 'Lemon Tea');
        proList.add(pro1);
        proList.add(pro2);
        insert proList;
        AutoCompleteController controller = new AutoCompleteController();
        controller.getjsonDescriptionData();
        controller.getjsonDescriptionData();
        BrowseCatalog brocat = new BrowseCatalog();
        AutoCompleteController controller1 = new AutoCompleteController(brocat);
        
    }
}

 

Tim BarsottiTim Barsotti

I would make a boolean like "Boolean throwerror = false" and set it to true the second time you call it with your test class.

 

try{
if(throwError) {
throw new MyCustomException(myMessage, ex);
} if(jsonDescriptionData == null){ jsonDescriptionData = new List<string>();
.........................

 

Then assert that the error was thrown with something like this:

ThrowError = true; 
Try {
  controller.getjsonDescriptionData();
} catch (exception e) {
system.assert(e.getMessage().contains('your error message'), e.getMessage());
}

 

Hope this helps!

 
Shailesh DeshpandeShailesh Deshpande
You can have one more testMethod. In this testMethod you needx to force an exception to occur, like the one which is suggested in the previous post. Or you can have some other conditions which will cause an exception to occur. For eg. Dont add the products in the proList and simply try to insert it. This should cover your catch part. You can identify a perfect condition based on your requirement and force the error.