0

I created an account, a contact and a user then connected all 3 to create a test community user in a method annoted with @testsetup. I ran my test in context of the community user created previously in setup method. But in my test methods when I try to fetch any data from logged in user's account(previously created in set up method), its returning null. However, I am able to fetch data from the logged in user's contact record. Below is a dummy code for my use case:

@isTest
Public class example_test{

   @testSetup
   Public static void createTestData(){
      
      //INSERT ACCOUNT
      Account a = new Account (Name = 'abc', Status = 'Active');
      insert a;

      //INSERT CONTACT
      Contact con = new Contact(Accountid = a.id, FirstName = 'X', LastName = 'Y');
      insert con;

      INSERT COMMUNITY USER
      User u = new User(LastName = 'testLast', Contactid = con.id, username = '[email protected]', email= '[email protected]' ..... );
      insert u;
   }

   @isTest
   Public static void test_Method1() {
       User testUser = [SELECT id,FirstName,LastName,Username,Contact.LastName,Contact.Account.Name from User LIMIT 1];
       system.assertEquals('Y',testUser.contact.LastName);
       system.assertEquals('abc',testUser.contact.Account.Name);

   }
}

In the above test class, for 1st assert statement it passed where as for the 2nd assert statement it failed as actual outcome is null. This problem is not occuring if you insert test data inside test methods or calling any test factory method not annoted as @testsetup Has anyone faced this problem before. Is this a problem for @testsetup methods?

4
  • It passes for me fine. Are you sure its passing for the first assert and failing for 2nd. Based on your code it should fail on this line only [SELECT id,FirstName,LastName,Username,Contact.LastName,Contact.Account.Name from User]; as there will be more than one user in system and a list cannot be assigned to an object. Jan 13, 2021 at 12:15
  • N.B. your assert args are backwards; first arg is expected value
    – cropredy
    Jan 14, 2021 at 0:50
  • @NagendraSingh - That was just a dummy code. I missed the LIMIT clause. Added that in post. But issue still persists. You can try in your dev once. Problem is, you will be able to fetch only the user data but when you try to fetch contact data or account data using relationship field it will only return null. Jan 14, 2021 at 9:15
  • @cropredy good catch. Corrected that. But that does not solve the underlying problem Jan 14, 2021 at 9:16

1 Answer 1

0

I am able to get the values in test scope too. So the theory might be like this.

There are some setup objects which can be queried from test class even if you have marked the test as SeeAllData as false. Documentation

So when you fire [SELECT id,FirstName,LastName,Username,Contact.LastName,Contact.Account.Name from User LIMIT 1] it is not guaranteed that you will get the user which you created above.

To do that you need to add something in where clause.

[SELECT id,FirstName,LastName,Username,Contact.LastName,Contact.Account.Name from User where username = '[email protected]'].

Try adding this and check if the issue persists.

1
  • you were correct. It was querying some org user data and hence the results were failing. I added the username condition in WHERE clause and it worked. Thanks ton !!! Jan 15, 2021 at 11:06

You must log in to answer this question.

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