I am writing a test class for my code and am running into an issue that is annoying me. So I was wondering if there is any way to do this.
I have an Apex class with some functionality that I am Unit Testing, let's assume it is defined something like this:
public class HelperClass {
public static Object someFunction(Parent__c parentObject) {}
}
The parentObject SObject input is expected to be provided in such a way, that it will contain the parent record as well as the child records so they are passed as one input and stay together. This is done by the calling methods by using a relationship query similar to the following way:
Parent__c parentObject = [SELECT Id, Name, (SELECT Id, Name FROM Children__r) WHERE Id = :recId];
Parent__c and Child__c are both configuration objects so for testing I have a pretty good idea what they should typically look like to fit my test cases. I want to see if it is possible to skip doing the DML as I want fast test classes and it are quite a few records that I want to create for all assertions. So I wanted to build the test data as below:
@IsTest
private static void testHelperClassSomeFunction() {
// define children records
List<Child__c> testChildren = new List<Child__c>{new Child__c(Name = 'Test Child Record')};
// define parent record
Parent__c testParent = new Parent__c(Name = 'Test Parent Record');
// add children records to the relationship:
testParent.Children__r = testChildren;
Object testOutput = HelperClass.somefunction(testParent);
// asserts here
}
However, if I do this, I get the error
Field is not writeable: Parent__c.Children__r
I know that I can insert the children and the parent and then use a relationship query. As mentioned I want to keep from doing the DML and requerying the records as this would otherwise be needlessly slow and this will be repeated in other tests as well.
Plus, I just want to know if there is any way around this... So is there any way to build the object as described above in Apex without fetching the data using a query?
Test.loadData()
can be used to set (some) otherwise read-only fields, but I don't know whether or not it would work. I'll aim to do some research if I can find some time later today.