We have a few triggers on different objects that depend on a custom setting to exist, so when we create test units on those objects, we need to create that custom setting.
We wanted to avoid having to manually create the custom setting on every test method so I thought of creating a method in a utility class to create this custom setting.
The problem is that sometimes when we run methods from the "Run All Tests" or the apex test execution (in parallel mode), we get an error on those methods "Unable to Lock Row".
The problem is that the different tests methods on different test classes try to create the same custom setting at the same time and the only way we have of preventing that is disabling parallel mode on the test execution page.
This is snippet of the method causing the issue, is there a better way to handle this?
Thank you.
public static void initCustomSettings() {
Custom_Privleges__c custp = Custom_Privleges__c.getValues(UserInfo.getProfileId());
if(custp==null){
Custom_Privleges__c cp = new Custom_Privleges__c();
cp.Override_All_Constraints__c=true;
cp.Integration_User__c=true;
....
....
insert cp;
}
}