If I'm building a unit test with a fair bit of overlap (for redundancy), and they all create leads for test purposes (without seealldata), at which point do those created objects get destroyed?

Is it when the method exits? Or when the Test Class exits?


2 Answers 2


The best way to understand the execution context is just assume a static apex method behaviour. Static methods don't have any state. Every thing executed within method is state full until the method exits.

Same thing happens in test classes everything executed between test methods block remain only for single transaction. Once the last execution finished everything reset to default. The only difference is test methods don't commit anything physically to database.

But during execution make sure that default behaviour of "run all test" is it executes all test methods in parallel so if there is a shared static method that query or insert record in object or custom setting then you may get exception due to conflicts.


When the method exits. Each test should run in it's own context. However, in Spring '15 @testSetup allows your tests to share data:


  • 1
    But that shared data is reset at the end of each method. The records remain the same as they were when each test starts. It simply reduces the total execution time.
    – sfdcfox
    Feb 16, 2015 at 19:37

You must log in to answer this question.

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