+ Start a Discussion
Apex developer 21Apex developer 21 

how di i catch expectedExceptionThrown in unittest

I have a unittest which runs fine on Contentversion exept the .adderror is not covered in my trigger. How do i catch expectedExceptionThrown where as with an system debug i get: DEBUG|link.LinkedEntityId ContentDocumentLink:{LinkedEntityId=0013E00000COrXWQA1, Id=06A3E0000000eVkUAI}. Here is my unittest:
@IsTest(seeAllData=false)
private class TestNoteOnContentversion {
	
    @IsTest 
    static void refuse_shouldAddError_whenOrderregelIsLocked() {
        // arrange
        Account acc = new Account(
        	Name = 'TEST_ACCT', 
            Account_Status_DS__c = 'Status'
        );
        insert acc;
        
        Orderregel__c orderregel = new Orderregel__c(
            Account__c = acc.Id,
            Orderbegindatum__c = Date.today()
        );
        
        insert orderregel;
        
    	ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        content.origin = 'H';
    	insert content;
    	
        ContentDocumentLink link=new ContentDocumentLink();
        link.LinkedEntityId=acc.id;
        link.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        link.ShareType = 'V';
       
    	insert link;
		
        //Id parentId = link.LinkedEntityId;
        
        // Query the Orderregels to lock
        Orderregel__c[] ord = [SELECT Id from Orderregel__c WHERE Account__c  = : acc.Id];
        
  		// Lock the accounts
		Approval.LockResult[] lrList = Approval.lock(ord, false);
        
		Test.startTest();
        try { 
              update content;
            }
        catch (Exception dmx) 
        { 
           Boolean expectedExceptionThrown = dmx.getMessage().contains('link.LinkedEntityId,Id') ? true : false; 
           system.debug(expectedExceptionThrown);
            System.assertEquals(expectedExceptionThrown, false);  
        }
        Test.stopTest();
    }   
}

 
bob_buzzardbob_buzzard
It looks like you have code to cover this, based on caturing the DMLException. Is the issue that you aren't capturing the exception, or you aren't able to generate the scenario where the error gets added to the record?
Apex developer 21Apex developer 21
The issue is that i cant generate the scenario, when i do a systemdebug i get: link.LinkedEntityId ContentDocumentLink:{LinkedEntityId=0013E00000COrXWQA1, Id=06A3E0000000eVkUAI} so how do i write in the contains: contains('link.LinkedEntityId,Id') line 49
Amit Singh 1Amit Singh 1
Hi,

If you want to cover Catch clock from test class and you are not able to generate the scenario which is responsible for covering the catch block.

Add the below code after update statement.
 
After // update content; statement add below code.

If(Test.isRunningTest()) Integer i = 10/0;

Hope it will help cheers :)

Thanks,
Amit Singh.