5

When running all test classes from the Setup menu in a Sandbox org at the same time, I am receiving the error "System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record:" on occasion which causes the test class to fail.

If I rerun one of these failed tests by itself, the error does not occur. However, I am concerned with what will happen when I go to move these tests to the Production org. Will this locking issue be an issue there as well? Or should I be making a code update to prevent this from happening?

John_Henry
  • 931
  • 4
  • 12
  • 39

1 Answers1

8

Try checking Disable Apex Parallel Testing under Setup/Develop/Apex Test Execution/Options.

enter image description here

I've encountered this before, and Daniel Ballinger's excellent post here has helped me out.

As Daniel acknowledges in this post, this is a more immediate fix. Ideally, you would be recreating a "clean room" testing scenario with each test, and recreating the database. This exception is being thrown because two tests are attempting to operate on the same record at once.

Brian Mansfield
  • 6,431
  • 5
  • 37
  • 69
  • 2
    Sometimes, it's also just a lock on the object itself. For example, trying to upsert custom settings that have the same name in each test may also cause this error, even if SeeAllData=false. The more heavily you rely on custom settings, the more likely it is that you'll need to disable parallel testing to get around this error. – sfdcfox Dec 09 '15 at 19:43