apex-enterprise-patterns / fflib-apex-common

Common Apex Library supporting Apex Enterprise Patterns and much more!
BSD 3-Clause "New" or "Revised" License
913 stars 517 forks source link

Apex Mocks test error for ContentDocumentLink Domain class #329

Closed PseudoDarwinist closed 1 year ago

PseudoDarwinist commented 3 years ago

We have been using FFLIB(Apex commons) in our org.I have been trying to write a test case for a Content Document Link (Salesforce Files)Domain layer But I have been getting the error : "System.NullPointerException: Argument cannot be null." I am new to the library and now that I have tried few things in the test class , I am assuming it is something to do with ContentDocumentLink. Can somebody please help me resolve this.I want to adopt the AEP and Apex mocks and not go back to writing usual test cases.

My class:

public Map<Id, String> getBodyAsStringByLinkedEntityId (){
        Map <Id, String> bodyAsString = new Map<Id, String>();
        for(ContentDocumentLink file : (List<ContentDocumentLink>) Records){
            Blob FileBody = file.ContentDocument.LatestPublishedVersion.VersionData;
            String attachmentAsString = FileBody.toString();
            bodyAsString.put(file.LinkedEntityId, attachmentAsString);
        }
        return bodyAsString;
    }

My Test Class:

// Given
        ContentDocumentLink record = new ContentDocumentLink(
            Id = fflib_IDGenerator.generate(ContentDocumentLink.SObjectType)
            );

    List<ContentDocumentLink> testContentDocumentLinkRecords = new List<ContentDocumentLink> { record };

    // When
    XYZ_DOM_IContentDocumentLinks contentDocumentLinks = XYZ_DOM_ContentDocumentLinks.newInstance(testContentDocumentLinkRecords);

        contentDocumentLinks.getBodyAsStringByLinkedEntityId();

    // Then
        System.assertEquals(1, contentDocumentLinks.getBodyAsStringByLinkedEntityId().size());
wimvelzeboer commented 3 years ago

@PseudoDarwinist Can you post the entire stacktrace of the error? I think the null exception is that you try to reference file.ContentDocument.LatestPublishedVersion.VersionData;, but there is no data in the record other than an ID. (See GIVEN part of the unit test)

PseudoDarwinist commented 3 years ago

@wimvelzeboer Below is the stack Trace: -- | -- Class | XYZ_DOM_ContentDocumentLinks_Test Method Name | testGetBodyAsStringByLinkedEntityId Pass/Fail | Fail Error Message | System.NullPointerException: Argument cannot be null. Stack Trace | Class.IRR_DOM_ContentDocumentLinks.getBodyAsStringByLinkedEntityId: line 13, column1 Class.IRR_DOM_ContentDocumentLinks_Test.testGetBodyAsStringByLinkedEntityId: line 33, column 1

PseudoDarwinist commented 3 years ago

Hi @wimvelzeboer ..Is there any way to mock CDL record with FFLIB so that I have the content version data? or Do i need to go usual DML way to test this Domain class? Help me out here.

cropredyHelix commented 3 years ago

Isn't this resolved as per Stackexchange answer ??

daveespo commented 1 year ago

If the workaround offered by @cropredyHelix isn't workable, please reopen the ticket and provide a little more context since it's not clear which line is line 13 in the stack trace