1

I have two test methods and a static variable. I am setting the value of the variable in one test method and want to access this value in the second test method. Is this possible?? Is there any other solution to access it? Thanks in advance.

 public static string oppId='';
    static testMethod void testConvertToLoanApp() {

    Test.startTest();
    String e = PODetails_SF1ControllerClone.ConvertToLoanApp();  
    List<Opportunity> op = [SELECT id from Opportunity where  Account.PANNumber__c =: 'AWEPT9807P' limit 1];
    if(op.size() > 0){
      oppId = op[0].id; //setting oppId here
    }


  static testMethod void testdomcpcheck(){
  //oppId here is null always
  Test.startTest();  
  PODetails_SF1ControllerClone.domcpcheck(oppId);
  Test.stoptest();
 }
1
  • 1
    In short, no you can not do that. test method runs in different transactions resetting any static variable. What are you trying to achieve? If you need any static variable value in 2 different functionality, you should write a single testmethod for them. Commented Feb 2, 2017 at 11:08

1 Answer 1

2

There is no way as by design individual test methods are isolated from each other. This allows them to be run in any order or singly.

You can factor out code that you want to use in more than one test method into a separate static non-testmethod annotated method and call that method from the testmethod annotated methods.

There is also the @TestSetup mechanism that can sometimes be helpful to setup shared data, though references to that shared data have to be found by querying.

You must log in to answer this question.

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