You need to sign in to do that
Don't have an account?
Shiv 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); } }
I would make a boolean like "Boolean throwerror = false" and set it to true the second time you call it with your test class.
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!