function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kelly KKelly K 

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:


Appreciate the help. - Kelly


Here's what's in the trigger I'm trying to test:

            catch (System.DMLException e) {
                for (Contract contract : 


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,,
					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));


Dhaval PanchalDhaval Panchal
copy exact status code from exception (get exception manually) and use that in system.assertEquals.
Kelly KKelly K

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? 

trigger ContractTrigger on Contract (after insert, after update) {

	if(Trigger.isAfter) {
    	if (trigger.isInsert) {
    		list<Contract> contractsToCreateEnrollKOCallTask = new list<Contract>();
    		for(Contract contract : {
    			//Contract Enroll KO Call Task Criteria
    			if(contract.Client_ID__c != null && contract.Go_Live_Date__c == null && contract.New_Scoreboard_under_existing_Site__c == false && contract.No_Kick_Off_Call_Needed__c == false)
    				if (contract.Implementation_Number__c == null) 
						contract.addError(' This contract does not have an implementation record attach to assign tasks. Please add an implementation or remove the \'Client ID\' to continue.  Contact an administrator for more information.');
					else if (contract.Enrollment_Rep__c == null) 
						contract.addError(' This contract does not have an enrollment rep set.  Please set the enrollment rep or remove the \'Client ID\' to continue.  Contact an administrator for more information.');

					else contractsToCreateEnrollKOCallTask.add(contract);
			try {
				if(contractsToCreateEnrollKOCallTask != null)
            catch (Exception e) {
                for (Contract contract :