[SalesForce] UNABLE_TO_LOCK_ROW error in parallel apex tests

i get an UNABLE_TO_LOCK_ROW error when running my tests in parallel. The error occurs when creating a pricebookentry for the standard pricebook. As far as i know Salesforce locks also the parent Object when inserting a object. Which would mean that in this case while inserting the pricebookentry the pricebook is locked. When now a parallel test wants to add another entry to the locked pricebook the test throws the UNABLE_TO_LOCK_ROW error.

When not running in parallel all the tests passes. But since the test suite is quite big i really would like to run them in parallel in order to save time.

Does anyone know how to get around this?

Best Answer

Place all of your tests that involve the Standard Price Book into a single class, and use @testSetup to create the products/price book entries you need. This way, the singular "test copy" of the Standard Price Book will only be locked once. You can place all of your other tests into separate classes. Tests do not run in parallel within a single class, so you'll avoid the row lock.

Related Topic