I have to test the results of a query like this:

[SELECT Id, Owner.Name FROM MyObject__c]

which is called in the constructor of a visualforce page. The Owner.Name is diplayed in the page. The page works fine and I can read the names.

In the unit test though...

User u = [SELECT Id, Name From User..]; //some User
    testObj = new MyObject__c(Field__c='test',OwnerId=u.Id);
    insert testObj;

..test goes on, the contructor of the page is called and finally the query arrives:

MyObject o = [SELECT Id, Owner.Name, OwnerId FROM MyObject__c WHERE..]

//o.OwnerId = correct Id
System.assertEquals(u.Id, o.OwnerId); //ok

//o.Owner = null
//o.Owner.Name = null
//...how does this work?
System.assertEquals(u.Name, o.Owner.Name); //not ok

Any insight?

Thank you very much.

  • Are you doing this query MyObject o = [SELECT Id, Owner.Name, OwnerId FROM MyObject__c WHERE..] in the test class?
    – Rao
    Commented May 16, 2013 at 16:56
  • No, it is done in the controller of the page, I create one controller instance in the test. (but yes, I also tried that query in the test, with the same result)
    – T. Rossi
    Commented May 16, 2013 at 17:00

1 Answer 1


salesforce known bug :

None of the owner fields are accessible in a test class


Interesting fact 1: when you use myobject.ownerid assertion passed but when you do myobject.owner.id assertion fails

System.AssertException: Assertion Failed: Expected: 005d0000001U4iVAAS, Actual: null

Interesting fact 2: field name typo in the bug heading :P

While Using SeeAllData=false, owner filed not visible in SOQL on Custom Objects

  • what about in 2023? Commented Nov 10, 2023 at 14:22
  • Still seems to be an issue in 2024. :( And since SeeAllData=true is not allowed if there is a TestSetup method it makes unit testing logic that uses related user fields cumbersome to say the least..
    – Cnork
    Commented Jun 12 at 8:56

You must log in to answer this question.

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