You need to sign in to do that
Don't have an account?
Exception Testing not providing code coverage
Hi All,
I'm trying to bump up the code coverage on one of my triggers and I'm finding that my negative test case is not providing code coverage and I'm not sure why. Still new to negative case testing, can anyone shed some light on what I'm overlooking?
Also, can anyone point me to an article or explain what exactly the Exception class includes (I'm not sure why you would use catch(Exception e) versus any of the ones listed here: http://www.salesforce.com/us/developer/docs/apexcode/index.htm)
Appreciate the help. - Kelly
Here's what's in the trigger I'm trying to test:
catch (System.DMLException e) { for (Contract contract : trigger.new) contract.adderror(e.getMessage()); }
Here's my test method (it passes, just doesn't cover the above code on my trigger). I attempt to throw the DMLexcpetion by leaving a required field (by validation rule) blank.
//Leave Client Diagnostic intentionally blank to see if it catches an error static testMethod void errorCheck(){ Account vendorAccount = new Account(Name = 'Vendor Acct', Status__c = 'Vendor', Type__c = 'Vendor'); insert vendorAccount; Go_Forward_PM_System__c goForwardPM = new Go_Forward_PM_System__c (Name = 'Greenway', Status__c = 'Partner', Client_Type__c = 'Channel', Vendor_Account__c = vendorAccount.Id); insert goForwardPM; Account account = new Account(Name = 'Test Account', Status__c = 'Backlog', Type = 'Medical Practice', Go_Forward_PM_System__c = goForwardPM.Id, Number_of_Docs__c = 1, Leaving_Which_Clearinghouse__c = 'Cerner', Current_Clearinghouse__c = 'Cerner'); insert account; Opportunity opportunity = new Opportunity(Name='Test Opportunity', AccountId = account.Id, CloseDate=System.today(), Type='New Sale (Bundle)',StageName = 'Demo Scheduled',Sales_Commit__c='Commit', RecordTypeId ='01230000000Jcoh', Client_Services_select_one__c ='Not Applicable'); insert opportunity; //Insert contract with a product election value Contract contract = new Contract(AccountId = account.Id, Opportunity_Name__c = opportunity.Id, Product_Election__c = 'Connect'); try { insert contract; } catch(DMLException e) { system.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION, e.getDmlType(0)); } }
Thanks for the reply Dhaval. Apologies it's taken me some time to get back to you, but I finally cleared up some time to look back into this.
I tried your suggestion and still no coverage. Perhaps I've set this up incorreclty? I have my contract trigger setup similar to this, is it incorrect to have the error catching in the trigger? Is that why I cannot get coverage?