Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fflib_ApexMocksUtils.makeRelationship does not support some standard objects? #59

Closed
yurybond opened this issue Dec 13, 2017 · 1 comment

Comments

@yurybond
Copy link

yurybond commented Dec 13, 2017

I am experiencing a problem when try to make relationship with two standard objects Folder and Document.

Below the code I am using:

    List<Folder> folders = new List<Folder>{
        new Folder(
            Id = fflib_IDGenerator.generate(Folder.SObjectType),
            Name = 'Test Public Folder'
    )};
    List<Document> allDocuments = new List<Document>{
        new Document(
            Id = fflib_IdGenerator.generate(Document.SObjectType),
            Name = 'Test Document',
            Type = 'pdf',
            FolderId = folders[0].Id
    )};
    folders = (List<Folder>) fflib_ApexMocksUtils.makeRelationship(
        List<Folder>.class, 
        folders, 
        Document.FolderId, 
        new List<List<Document>> {allDocuments}
    );

The exception I get is:

    12:56:40:000 FATAL_ERROR Class.System.JSONGenerator.writeFieldName: line 58, column 1
    12:56:40:000 FATAL_ERROR Class.fflib_ApexMocksUtils.InjectChildrenEventHandler.nextToken: line 113, column 1
    12:56:40:000 FATAL_ERROR Class.fflib_ApexMocksUtils.streamTokens: line 135, column 1
    12:56:40:000 FATAL_ERROR Class.fflib_ApexMocksUtils.makeRelationship: line 85, column 1
@dfruddffdc
Copy link
Contributor

dfruddffdc commented Dec 28, 2017

Yup, I'm able to replicate this. Full stack appears to be:

 ▸    ERROR: System.NullPointerException: null argument for JSONGenerator.writeFieldName()
 ▸    ERROR: Class.System.JSONGenerator.writeFieldName: line 58, column 1
 ▸    Class.fflib_ApexMocksUtils.InjectChildrenEventHandler.nextToken: line 113, column 1
 ▸    Class.fflib_ApexMocksUtils.streamTokens: line 135, column 1
 ▸    Class.fflib_ApexMocksUtils.makeRelationship: line 85, column 1
 ▸    AnonymousBlock: line 13, column 1
 ▸    AnonymousBlock: line 13, column 1

I also tried your code sample, switching Folders with Accounts, and Documents with Contacts. It works. The trouble is that there is no relationship field name for the child relationship from Folder to Document.

// => FolderId relationship name: Folder
System.debug('FolderId relationship name: ' + Document.FolderId.getDescribe().getRelationshipName());

for (Schema.ChildRelationship r : SObjectType.Folder.getChildRelationships())
{
    if (r.getField() == Document.FolderId)
    {
        // => ChildRelationship Name: null
        System.debug('ChildRelationship Name: ' + r.getRelationshipName());
    }
}

So the utility can't match Folder's child relationship to Documents parent relationship, hence your error. But even if it did, would it help? I don't think you'd be able to traverse the child relationships in the non-test Apex code, because there is relationship name for you to get a Folder's child Documents.

Closing as it seems like a Salesforce Platform limitation, rather than an issue with ApexMocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants