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
William Woodson 3William Woodson 3 

Why do I see contacts in unit test, not using seeAllData

For some reason, my Contact object still shows existing contacts inside my unit tests even though I am not using seeAllData=true...
 
@isTest
public class TravelDetailGateway_Tests {
    @isTest
    public static void loadData_Tests() {
        Test.loadData(Account.sobjectType, 'test_data_accounts'); //file contains 10 rows.
        //contact in file is associated to account 9 in the file above.
        Test.loadData(Contact.sobjectType, 'test_data_contacts'); //file coontains 1 row.
        
        integer acctCount = [SELECT count() FROM Account];
        integer ctcCount = [SELECT count() FROM Contact];
        
        system.assertEquals(10, acctCount);
        system.assertEquals(1, ctcCount); //assertion fails and returns 11 records.
    }
}

 
Best Answer chosen by William Woodson 3
Maharajan CMaharajan C
Williams check is there any Apex trigger or Process Builder or Flow is available to create the contact record automatically when the account is inserted.

Or 

Check the data factory class properly is there any other contact insertion is happening when insert the account or any loop is there to insert the multiple contacts ( here your data factory class name is: Test)

Thanks,
Maharajan.C

All Answers

TechingCrewMattTechingCrewMatt
Hello, William. Is it possible you are loading Person Accounts when you are loading Accounts?

Thanks,
Matt
William Woodson 3William Woodson 3
No standard accounts
TechingCrewMattTechingCrewMatt
What happens when you try the following?
@isTest
public class TravelDetailGateway_Tests {
    @isTest
    public static void loadData_Tests() {
        System.assertEquals(0, [SELECT count() FROM Contact], 'Zero Contacts should exist - Line 5');
        Test.loadData(Account.sobjectType, 'test_data_accounts'); //file contains 10 rows.
        System.assertEquals(0, [SELECT count() FROM Contact], 'Zero Contacts should exist - Line 7');
        //contact in file is associated to account 9 in the file above.
        Test.loadData(Contact.sobjectType, 'test_data_contacts'); //file coontains 1 row.
        
        integer acctCount = [SELECT count() FROM Account];
        integer ctcCount = [SELECT count() FROM Contact];
        
        system.assertEquals(10, acctCount);
        system.assertEquals(1, ctcCount); //assertion fails and returns 11 records.
    }
}

 
Maharajan CMaharajan C
Williams check is there any Apex trigger or Process Builder or Flow is available to create the contact record automatically when the account is inserted.

Or 

Check the data factory class properly is there any other contact insertion is happening when insert the account or any loop is there to insert the multiple contacts ( here your data factory class name is: Test)

Thanks,
Maharajan.C
This was selected as the best answer
William Woodson 3William Woodson 3
@TechingCrewMatt, the result of your code is....
System.AssertException: Assertion Failed: Zero Contacts should exist - Line 7: Expected: 0, Actual: 10
TechingCrewMattTechingCrewMatt
The passed assertion at line 5 indicates that no Contacts exist in the dadatbase before the Account loading at line 6. After the Accounts are loaded, 10 Contacts exist. There must be a process creating Contacts. Would you please include the debug logs?
William Woodson 3William Woodson 3
Yep Maharajan and TechingCrewMatt are both right, there is a PB that is creating a Main Number contact on the account, got to love declaritive changes messing with my testing :) Thanks TechingCrewMatt, wish I could give you credit on the answer, but I can only select one and Maharajan called it.
Maharajan CMaharajan C
Thanks Willam !!!