You need to sign in to do that
Don't have an account?
Apex 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(); } }
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.
Hope it will help cheers :)
Thanks,
Amit Singh.