function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Roger WickiRoger Wicki 

SeeAllData does not seem to work on helper methods in test classes

Dear Community

I have following setup:
@isTest
private class myTestClass {
	/** static helper variables to store data across different test classes to
	/*  accelerate test speed
	*/
	Boolean isInitiated = false;
	//  ...

	@isTest(SeeAllData=true)
	private static void init() {
		// create helper variable's values
		// ...
	}

	static testMethod void testStuff() {
		if ( !isInitiated ) { init(); }
		
		// test all the stuff to be tested
	}
}
Within the init method I use nother helper class I worte independatly to create different sObjects. One of them relies on a custom setting. Now I know that SeeAllData has to be set to true so that the method can access that data, but I do not understand why it doesn't work for the case above. In the documentations, it is not said that the isTest Annotation with the SeeAllData addition may only be used for the actual testMethods... I guess this must be where it fails because otherwise it seems to work correctly...

Any clarification on that?
Best Answer chosen by Roger Wicki
Roger WickiRoger Wicki
I have switched to custom metadata types. They are always available and can simply be queried in a test class.

All Answers

Stefan AbramiukStefan Abramiuk
Hi Roger,
you should put this annotation on test method not helper method.
 
@isTest
private class myTestClass {
	/** static helper variables to store data across different test classes to
	/*  accelerate test speed
	*/
	Boolean isInitiated = false;
	//  ...

	private static void init() {
		// create helper variable's values
		// ...
	}

	@isTest(SeeAllData=true)
	private static void testStuff() {
		if ( !isInitiated ) { init(); }
		
		// test all the stuff to be tested
	}
}

 
AshlekhAshlekh
Hi,

But using SeeAll data is not a good practise and you need to create you dummy data in test class and then do wirte test class.

-Thanks
Ashlekh Gera
Roger WickiRoger Wicki
Hi Stefan

As I thought. But isn't that a bit counter-logic? Maybe I have to do some SOQL / DML within that test method and then allowing SeeAllData would be exactly not what I want...

@AKG
I know it is not best practice, but what for do I create list type custom settings then? They represent kind of a org wide resource base for data. The only alternative would probably be some static resources but then I'd have to write a file parser and could not use that data in formulas as well...

I use custom settings to expose data which frequently changes so I would not have to touch my code when some of the values alter...
Antonio ManenteAntonio Manente
Hey Roger, 

I understand your concern, and it's definitely good practice to utilize custom settings in that way. That being said, you are able to create new instances of custom settings in test methods/classes. So you don't need to use SeeAllData=true to utilize them.
Roger WickiRoger Wicki
Hi Antonio

True, but the data itself for custom settings is a pain to create. The biggest amount of data we have in our custom settings is about 250 records. That's still doable in test classes. but what if there were 2500? What if there were even more? I know I only need as much as the class / trigger needs to run properly, but if other processes depend on the exact values like in production I have to recreate all of those in every test class that uses that. And If I have a helper method that creates them for me, why would I even use a custom setting then at all?
Roger WickiRoger Wicki
I have switched to custom metadata types. They are always available and can simply be queried in a test class.
This was selected as the best answer