We've been seeing the UNABLE_TO_LOCK_ROW issue in our prod org. I've created a retry for DML actions facing this issue to sleep for a certain number of milliseconds between each try and try for a certain number of times. I'm unable to replicate the error within my unit tests. I'd like to know if it's possible to purposefully produce the UNABLE_TO_LOCK_ROW error in the unit test so I can test the retry logic?

1 Answer 1


You cannot intentionally cause this to happen, because you need at least two threads in order for this to actually get thrown. Two parallel tests running at once can accidentally cause this problem, but parallel testing is disabled for deployments, so those tests wouldn't pass correctly, even if you could coordinate two specific tests to run in parallel on purpose.

In other words, this is one of those times when you would need dependency injection or Stub API mocking. Either way, you'd have to have either a DML or SOQL class to inject code in to during testing. As I've advised before, it's best to probably just assume it works and leave out the coverage. In most cases, this shouldn't even drop you 1% of your total coverage if you're doing it right.

  • 1
    I had to do this once for some sophisticated retry logic; with a Selector pattern, an injected mocked selector, and ApexMocks doThrowWhen it is fairly easy to unit test your retry logic
    – cropredy
    Nov 12, 2021 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .