Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

1 Answer 1

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. –  nivyaj Aug 4 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 at 7:30

Your Answer

 
discard

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.