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?
[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.