Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In one of my test classes, I want an opportunity to persist across all the testmethods. how do I do that? Also, is that a best practice when it comes to apex?

share|improve this question
up vote 2 down vote accepted

You can not persist values across testmethods and is bad practice to do so. SFDC rolls back all updates at the conclusion of a testmethod

Instead, if you need an Opportunity in each testmethod, consider loading it with the Apex statement using a common-to-all testmethods mockEnv() method which you can query for in each testmethod and use as needed

private static void mockEnv() {
  Test.loadData(Opportunity.sObjectType,staticResourceTestDatasetName); // a csv file 
share|improve this answer
Ah, that's right! Thanks. – user11235813 Aug 4 '14 at 22:05
@nivyaj ...or sometimes it is useful to set the data up in a class so multiple created objects can be referenced and methods such as asserts that check several things can be written and then all this can be shared between tests - see Cleaner inner class test fixture pattern. – Keith C Aug 5 '14 at 7:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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