function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
voutchervoutcher 

Object relationships in a testMethod

Hopefully a quick question? How does one access relationships in a testmethod?

What I'm doing is building an environment something like:

Account a = new Account(Name='myaccount');
insert a;

customobj c = new customobj(Aname = 'Harry', Account = a.ID);
insert c;

controller.mymethodtobetested();

 

I want to do something like c.Account__r.Name in a SOQL query in mymethodtobetested() but the object relationships aren't built at that point because nothing is committed. For a user, there is no possibility that the relationships wouldn't be available at that point.

How do I test methods that use object relationships like this?

Thanks all.

 

osamanosaman

SOQL runs on the database and in test method nothing commits. try working out without the SOQL. Can you explain your issue that what would you want to query and what would you want to do with the result?

voutchervoutcher

Hi Osaman,

 

Basically I'm running a SOQL query in the function that I'm trying to test. That query uses an object relationship.

 

Here is some some simplified code:

 

 

public Site__c getManagingAccount(){
                List<Site__c> managingaccount = [SELECT ID, 
                                            Account_Name__c,
                                            Site_Inheritance__c,
                                            Street__c,
                                            Town_City__c,
                                            ,Organization__c
                                           
                                         FROM Site__c
WHERE Organization__r.Cert_Relation__c = :getCurrentRecordID() LIMIT 1];
        return managingorganization[0];
    }

 

But the site__c in the first place has only just been created by the test method - so that I can test retrieving something specific. Ordinarily this record is created by the previous page automatically so it's relationships would be available.

 

As I say it works fine in reality but when I test nothing is committed so what can I do here?

Pradeep_NavatarPradeep_Navatar

Use Account__c in place of account__r.name. In testmethod you can't give account__r.name.

 

Try the sample code given below :

 

customobj c = new customobj(Aname = 'Harry', Account__c = a.ID);

insert c;

controller.mymethodtobetested();   

 

Did this answer your question? if so, please mark it solved.                                                      

voutchervoutcher

Thanks for the replies after a few hours of trying to work around this by updating an existing record instead of inserting, I found a bug in my code.

 

However, I haven't found it documented anywhere that it seems that in testMethods object relationships ARE built even though data isn't committed which allows you to do what I was attempting.

 

Thanks Osaman.

crop1645crop1645

In testmethods,  objects become available to reference immediately after you insert them; similarly if you update or delete an object, it is updated or deleted.  However, the parent->child relationship isn't implicitly available, you'll need to requery to take advantage of that.  That is, inserting an Account and then inserting 3 Contacts for that Account doesn't make the relationship a.contacts implicitly available to the APEX code in the testmethod, you can only get that via a subsequent SOQL call.

 

Now of course, if you have triggers that alter the values in an object (or have formula/RSF fields), you'll need to re-query the object in the testmethod to see those field values.

 

Testmethods, when they terminate, rollback all database updates done during the testmethod.