3 answers
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
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 Account createTestAccount() {
Account acct = new Account(
AccountNumber = '123456'
);
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-intropublic 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;
}