I have a SandboxPostCopy class where I am updating some User fields (example: changing the email to remove the '.invalid' and setting Profiles) where the Users to be updated are controlled by Custom Metadata.

When I was creating the test class, I was worried that it would actually update the Users since they were controlled by the metadata which would actually be visible to the test. However, to my surprise, the test passed fine and it didn't actually update the User fields in the sandbox.

That made me wonder, does Test.testSandboxPostCopyScript avoid actually running DML such as insert and update?


Any unit test you ever run in Salesforce will roll back all DML Operations at the end of the transaction. Same with emails sent out. Unit tests do not persist any change to your org, other than incrementing auto-number fields unless you configure them to avoid it.

See, for example, from Add a Test Class in the Apex Developer Guide (emphasis mine):

The method validateHelloWorld is defined as a testMethod. This annotation means that if changes are made to the database, they are rolled back when execution completes. You don’t have to delete any test data created in the test method.

The exact same would hold true for using the alternative @IsTest annotation.

  • 2
    The only exception is Big Object test class. Any Apex tests that use DML calls will fail and potentially insert bad data into the target big object. This data won’t be deletable. To test DML calls, use a mocking framework with the Apex stub API to contain calls to the target big object. – Pranay Jaiswal Dec 13 '19 at 17:08
  • 2
    Oh man, that's scary. Didn't know that. – Adrian Larson Dec 13 '19 at 17:10
  • Ah that makes sense. I was thrown off because I recently had a unit test that covered User password resets using System.resetPassword and that did attempt to change the password of the real User so was worried this would be similar. – Developer Guy Dec 13 '19 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.