Skip to main content
Hi,

I have been getting a few  'unable_to_lock_row' exception when I  run Apex Test Execute  in Production or Sandbox  but when I execute  the test cases individually (unit test)  they pass.. Even a few of  the managed package test cases  fail due to this error.

A few of the local test classes all lock on the same object.

Since I have different  test classes that need to use this object to test different functionality, My Question is:

Q) Is there a way to avoid this error ?

Q) Would I have to do  some sort of asynchronization on the test cases in order for the cases to wait for until he object is free? If so, how do I do this?

 Thanks
3 answers
  1. Jan 9, 2015, 2:36 PM
    This comes from the fact that your test code likely uses the same "unique" identifiers in your test code.  For example if you had the following code to generate an account

     

    public static Account createTestAccount() {

    Account acct = new Account(

    AccountNumber = '123456'

    );

    insert acct;

    return acct;

    }

    and then you used that code in all of your tests, you would likely run into UNABLE TO LOCK ROW errors because the AccountNumber is suppose to be unique and when there are updates happening on the account object it tries to lock that object but will fail because the external identifier is the same.

    To work around this, you should create these external identifiers randomly and use those instead.  For example:

     

    public static Integer getRandomInteger(Integer base) {

    return Math.round(Math.random() * base);

    }

    public static Account createTestAccount() {

    Account acct = new Account(

    AccountNumber = String.valueOf(getRandomInteger(100000)

    );

    insert acct;

    return acct;

    }

    If you are not using a TestUtils [1] type class to generate your testing data, I would recommend doing it.  This way you would just have to update it in one place instead of all of your tests

    [1] http://pcon.github.io/presentations/testing/⌗testutils-intro
0/9000