8

I have a bunch of unit tests in which I'm updating my custom settings object to test certain workflows. This causes an UNABLE_TO_LOCK_ROW exception since multiple tests are updating the values simultaneously. If I choose 'Disable Parallel Running' in Options, I do not get any errors. I have the following questions with this approach:

  • Custom objects have a setting to provide a FOR_UPDATE option for Locking. Is there a way I can update custom settings with a lock using CustomSettings.getOrgDefaults()?
  • In case of installing the package, is there a way to specify 'disable parallel running'. If not, how would package installation work in this case as there would be test failures?
  • Tests always run in serial for deployment. About custom settings - got same issue, trying to do tests, that require custom settings modification, in single test method, one after one, and under runAs test user. – kurunve Mar 13 '17 at 20:27
  • "Tests always run in serial for deployment" - I'm not sure if this is true. I'm deploying my code using mavensmate and the deployment validation fails because the tests are running in parallel. – gerad26 Mar 13 '17 at 20:33
  • 2
    @gerad26 yup, it is true. But validation tests may be run differently than actual deployments. – Sebastian Kessel Mar 13 '17 at 20:46
  • I see, is that the case only for production or sandbox as well? Also, is it right to say that parallel test execution is only useful for developer productivity and adds no value in actual deployments? – gerad26 Mar 13 '17 at 21:35
  • 2
    Here is the documentation developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…. Please note the yellow box: Apex tests that run as part of a deployment always run synchronously and serially. – Sebastian Kessel Mar 13 '17 at 22:13
1

In theory, this issue should have no impact on deployments. It can still be nice to have parallel testing work as you intend.

I have had some luck with a utility method structured essentially as follows:

public static void setupConfig()
{
    if (HierarchySetting__c.getOrgDefaults().Id != null && 0 < [SELECT count() FROM HierarchySetting__c])
    {
        system.assertNotEquals(null, HierarchySetting__c.getOrgDefaults().SomeField__c);
        return;
    }
    system.runAs(ADMIN_USER)
    {
        insert new HierarchySetting__c(SomeField__c='Populated');
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.