Contents
How can we use static variable in static method?
You can’t declare a static variable inside a method, static means that it’s a variable/method of a class, it belongs to the whole class but not to one of its certain objects. This means that static keyword can be used only in a ‘class scope’ i.e. it doesn’t have any sense inside methods.
Why are static variables useful?
Static variables are used to keep track of information that relates logically to an entire class, as opposed to information that varies from instance to instance.
Why static method is bad?
The reason you are warned away from static methods is that using them forfeits one of the advantages of objects. Objects are intended for data encapsulation. This prevents unexpected side effects from happening which avoids bugs. Static methods have no encapsulated data* and so don’t garner this benefit.
When to set static variables in unit tests?
As the resources used to set up test data does not occur in a real scenario, the limits should be raised significantly on test setup code. There should probably be at least 3 or 4 limit contexts for unit tests: setup, before startTest (), during the test, after stopTest ().
Why are static variables allowed in Salesforce testsetup?
Salesforce could then apply some limit on the number of @TestSetup static variables are allowed in a test class in order to make sure the data management doesn’t get too complex. There could even be restrictions on the types of such variables too. E.g. only support singular and arrays of Ids and SObjects.
When to reset annotated variables to their collected value?
To add to that: such annotated variables should be reset to their collected value (their value at the end of execution of the test setup method) at the start of each test method execution, thereby side-stepping Josh Kaplan’s (somewhat bogus) concern about inter-test dependencies on data changes in the static values.
Why are static variables included in the testsetup annotation?
It would be convenient if the state of the heap (particularly all static members of classes that are used), would also be included in the snapshot that the testSetup annotation provides, thus reducing the number of queries we have to perform.