I used the post: Test class for contentdocumentlink as a model, however, when I attempt to define my own ContentSize for the ContentVersion document I receive:

Field is not writable: ContentVersion.ContentSize

ContentVersion contentVersion = new ContentVersion(
      Title = 'Penguins',
      PathOnClient = 'Penguins.jpg',
      VersionData = Blob.valueOf('Test Content'),
      IsMajorVersion = true,
      ContentSize = 304088000
    insert contentVersion;

In my trigger I'm making decisions predicated on the ContentSize of the file added - if I'm unable to define ContentSize, my code coverage is only 41%


Since the field is not writable I think you only have three options.

  • If the file you are comparing to is small, you can simply create a file of that size. In your case the file size requirement seems too big to do that. That leaves you with two other solutions.
  • You can use Test.isRunningTest() to determine if it is a test and test your logic.
  • Alternatively, you could use @isTest(SeeAllData=true) and use a real file to test your logic with.
  • Ended up using your third suggestion Aug 28 '17 at 16:48

Ended up going with Kasper's third bullet point and using:

@isTest(SeeAllData=true) private class Test_ContentDocumentLink { static testMethod void myUnitTest() {
// uses a testUtility class to pre-define required fields etc. Opportunity opp = TestUtils.CreateOpportunities(1)[0]; insert opp; // not preferred, but defining ContentSize while creating a ContentVersion record resulted in the error "field not writable"
// a file larger than 304087040 bytes must reside in the org - otherwise nothing will be returned in the query List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument WHERE ContentSize > 304087040 LIMIT 1]; for (ContentDocument contentDocument: documents){ //create ContentDocumentLink record ContentDocumentLink cdl = New ContentDocumentLink(); cdl.LinkedEntityId = opp.id; cdl.ContentDocumentId = contentDocument.Id; cdl.shareType = 'V'; insert cdl; } } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.