You need to sign in to do that
Don't have an account?
Scott Brady
Need help with test class - 31% coverage currently
Hi All,
I am in search of some assistance with helping me get a very simple controller into production. I currently have 31% test coverage but I am running into issues confirming a page reference (and perhaps I may be missing something else to test to get to 75% coverage). See my controller below:
Now here is my current test class in progress, sitting at 31% coverage at the moment. Any tips would be most appreciated! Thank you very much in advance.
I am in search of some assistance with helping me get a very simple controller into production. I currently have 31% test coverage but I am running into issues confirming a page reference (and perhaps I may be missing something else to test to get to 75% coverage). See my controller below:
public with sharing class dhrequestsInsertController { public dhrequest__c requests { get { if(requests == null) requests = new dhrequest__c(); return requests; } set; } public dhrequestsInsertController() { } public PageReference SubmitRequest() { try { insert requests; } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new request. Please contact the Business Intelligence Team.')); } PageReference detailPage = new PageReference('/' + requests.id); return detailPage; } public PageReference CancelRequest() { PageReference pr = new PageReference('/apex/dhrequests'); pr.setRedirect(true); return pr; } }
Now here is my current test class in progress, sitting at 31% coverage at the moment. Any tips would be most appreciated! Thank you very much in advance.
@isTest public class dhrequestsInsertControllerTest { public static testMethod void dhrequestsInsertControllerTest() { PageReference pageRef = Page.dhrequestdiscovery; Test.setCurrentPage(pageRef); dhrequestsInsertController controller = new dhrequestsInsertController(); dhrequest__c r = new dhrequest__c(dh_businesscase__c='test', dh_errormessage__c='error', dh_workaround__c='workaround'); System.debug('business case before inserting: ' + r.dh_businesscase__c); Insert r; String nextPage = controller.SubmitRequest().getUrl(); System.assertEquals('/' + dhrequest__c.id, nextPage); r = [SELECT dh_businesscase__c, id, dh_errormessage__c, dh_workaround__c FROM dhrequest__c WHERE id =:r.Id]; System.debug('business case after inserting: ' + r.dh_businesscase__c); System.debug('error after inserting: ' + r.dh_errormessage__c); System.debug('workaround after inserting: ' + r.dh_workaround__c); System.assertEquals('test', r.dh_businesscase__c); System.assertEquals('error', r.dh_errormessage__c); System.assertEquals('workaround', r.dh_workaround__c); String lastPage = controller.CancelRequest().getUrl(); System.assertEquals('/apex/dhrequests', lastPage); } }
the SubmitRequest() pageref is giving me fits because I need to confirm that the record that is inserted, it's record Id is contained in the submitrequest redirect (to the detail page of the customer object). I am getting mismatches there. I also do not know how to replicate the error message itself, since it's really a catch all. Any ideas? Thanks again in advance.
Best,
Scott
All Answers
Let us know if this will help you
Thanks again for your help everyone!