0

I'm trying to mock out something similar to the following structure:

  • Task.LastModifiedBy.Username;
  • Task.LookupRelationshipToUser__r.Username;

Using the makeRelationships method for this doesn't make sense to me so far. I've used the makeRelationships method to mock out parent to child relationships successfully for mocking out queries but I'm at a loss with on how to proceed with this method.

Using the setReadonlyFields allows me to set Task.LastModifiedById to a mocked Users Id, but then when my code attempts to reference Task.LastModifiedBy.UserName I get null pointer errors while running test. I've tried using this method to set Task.LastModifiedBy.Username but I get error saying it doesn't recognize LastModifiedBy while deploying.

Maybe my terminology is off so searching around and looking at the FFLIB/Apexmocks resources hasn't been helpful so far. If anyone has any advice or can point me in the direction of some examples or documentation I would appreciate it.

4
  • 1
    Have you tried deserializing a string into the object with the lookups set? Better yet there's this library that lets you do this github.com/mattaddy/SObjectFabricator Mar 23, 2023 at 13:42
  • I've tried to convince people to add SObjectFabricator to our codebase but it falls on deaf ears. I'm going to start searching around and testing out the deserializing methods and see what I come up with. I was stuck on the idea that there was a method in Apexmocks that would do this. Thank you for your response. Mar 23, 2023 at 14:10
  • Let me write up an example on how to do it. Mar 23, 2023 at 14:14
  • SObjectFabricator is great stuff -- I've used it for years without issue and is essential for easy-to-read mocks of hierarchies including autonumber, formula, and system audit-type fields
    – cropredy
    Mar 23, 2023 at 15:55

1 Answer 1

1

In response to your comment about not being able to use SObjectFabricator, you can deserialize a string into an object with these fields/relationships set.

This is pretty crude, but it works. Use it as a starting point.

Group_Member__c groupMember = new Group_Member__c(
    Id = TestUtils.generateId(Group_Member__c.SObjectType),
    Group__c = TestUtils.generateId(Group__c.SObjectType)
);
String groupJSON =
    '{"attributes": {"type": "Group__c"}, "Id": "' +
    groupMember.Group__c +
    '", "Name": "Test Group", "Group_Members__r": {"done": true, "totalSize": 1, "records":' +
    JSON.serialize(new List<Group_Member__c>{ groupMember }) +
    '}}';
Group__c mockGroup = (Group__c) JSON.deserialize(groupJSON, SObject.class);

This will serialize into a custom Group__c object with its child relationship Group_Members__r populated with the Group_Member__c records.

There are some other posts around this site that explain the format:

How to deserialize JSON to sObject?

Deserialize JSON to SObject

1
  • You've given me a lot to work with, thank you! Mar 23, 2023 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .