You need to sign in to do that
Don't have an account?
Nick Joseph
Code Coverage for Catch Block
All,
I've reviewed several examples and cannot get any to work for me. Here is Controller, complete with Test Class. Any advice for how to get my Catch Block covered in this scenario.
I've reviewed several examples and cannot get any to work for me. Here is Controller, complete with Test Class. Any advice for how to get my Catch Block covered in this scenario.
public class EscalateToDataController { private final Case datacase; private final Case supportcase; private String datacaseURL; private String supportcaseURL; public EscalateToDataController() { datacase = [SELECT ID, Subject, Recently_Escalated__c, Escalated_To_Data__c, Reverted__c, Type, OwnerID, Data_Case_Description__c, Status, Description, Priority, Severity__c,Primary_Case_Team_Member__c FROM Case WHERE Recently_Escalated__c = TRUE AND (Escalated_To_Data__c = TRUE OR Reverted__c = TRUE)]; try { supportcase = [SELECT ID, Subject, Recently_Escalated__c, Escalated_To_Data__c, Reverted__c,Type, OwnerID, Description, Status, Priority, Severity__c, Data_Case_Description__c,Primary_Case_Team_Member__c FROM Case WHERE Recently_Escalated__c = TRUE AND Status = 'New']; } catch (Exception ex) { supportcase = null; } datacaseURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + datacase.Id; if (supportcase != null) supportcaseURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + supportcase.Id; } public Case getDataCase() { return datacase; } public Case getSupportCase() { return supportcase; } public String getDataCaseURL() { return datacaseURL; } public String getSupportCaseURL() { return supportcaseURL; } public void resetCases() { List<Case> casesToReset = new List<Case>(); datacase.Recently_Escalated__c=FALSE; datacase.Reverted__c=FALSE; if(supportcase != null) { supportcase.Recently_Escalated__c=FALSE; casesToReset.add(supportcase); } casesToReset.add(datacase); update casesToReset; } }
@isTest public class EscalateToDataControllerTest { public static testMethod void testController() { List<Case> caseList = new List<Case>(); User testOwner = [SELECT ID FROM User WHERE Controls_Data_Case_Queue__c=TRUE]; if (testOwner.ID==NULL) { testOwner = [SELECT ID FROM User WHERE Alias='Nick']; } String testOwnerID = testOwner.ID; String dataURL; String supportURL; Case testDataCase = New Case ( Subject = 'Test Subject', Type = 'Escalated', Recently_Escalated__c = TRUE, Escalated_To_Data__c = TRUE, OwnerID = testOwnerID, Data_Case_Description__c = 'Test', Reverted__c = FALSE, Description = 'Test', Priority = 'High', Severity__c = 'Urgent', Status = 'Escalated - Data', Primary_Case_Team_Member__c = '0051400000Bogfv' );insert testDataCase; caseList.add(testDataCase); Case testSupportCase = New Case ( Subject = 'Test Subject2', Type = 'Question', Recently_Escalated__c = TRUE, Reverted__c = FALSE, Escalated_To_Data__c = FALSE, OwnerID = testOwnerID, Data_Case_Description__c = 'Test2', Description = 'Test2', Priority = 'High', Primary_Case_Team_Member__c = '0051400000Bogfv', Severity__c = 'Urgent', Status = 'New' );insert testSupportCase; caseList.add(testSupportCase); Case testControlCase = New Case ( Subject = 'Test Subject2', Type = 'Configuration Change', Recently_Escalated__c = FALSE, Escalated_To_Data__c = FALSE, Reverted__c = FALSE, OwnerID = testOwnerID, Data_Case_Description__c = 'Test2', Primary_Case_Team_Member__c = '0051400000Bogfv', Description = 'Test2', Priority = 'High', Severity__c = 'Urgent', Status = 'In Progress' );insert testControlCase; caseList.add(testControlCase); EscalateToDataController controller = new EscalateToDataController(); controller.getDataCase(); controller.getSupportCase(); dataURL = controller.getDataCaseURL(); supportURL = controller.getSupportCaseURL(); PageReference ref = new PageReference('/apex/EscalateToDataFinish'); System.assertEquals(testDataCase, controller.getDataCase()); System.assertEquals(testSupportCase, controller.getSupportCase()); System.assertEquals(dataURL, controller.getDataCaseURL()); System.assertEquals(supportURL, controller.getSupportCaseURL()); Test.startTest(); List<Case> newCaseList = new List<Case>(); controller.resetCases(); newCaseList = [SELECT ID, Recently_Escalated__c, Reverted__c FROM CASE WHERE Recently_Escalated__c=TRUE OR Reverted__c = TRUE]; System.assertEquals(0, newCaseList.size()); Test.stopTest(); } }
If you want to test your exception handling, you will need to add a second test method in which you only create data for the datacase attribute. The try/catch block will generate the "List has no rows for assignment to SObject" error, since you did not add data for supportcase.
Please see the added testController_generateException method in below class for an example.
Just a tip: try to avoid hardcoding ID's. This article explains why:
https://developer.salesforce.com/page/Best_Practice:_Avoid_Hardcoding_IDs
Hope this helps!
Best regards,
Martijn Schwärzer