Yes, you must re-query the data. Sometimes you might use constant strings to pull in, for instance a custom setting:
static final String SETTING_NAME = 'foo';
@TestSetup
static void setup()
{
insert new MySetting__c(Name=SETTING_NAME);
}
static testMethod void testBar()
{
MySetting__c relevantSetting = MySetting__c.getInstance('foo');
}
Often, this approach is used for configuration data that will be common to your tests, be it in a Custom Object
or Custom Setting
.
Similar to your use case, it's a nice convenience when testing an extension.
@TestSetup
static void setup()
{
insert new Case(/*data*/);
}
static StandardController getController()
{
return new ApexPages.StandardController([SELECT ... FROM Case LIMIT 1]);
}
static testMethod void testMyExtension_Constructor()
{
ApexPages.StandardController controller = getController();
Test.startTest();
MyExtension extension = new MyExtension(controller);
Test.stopTest();
// assert on behavior
}
static testMethod void testSomeMethod()
{
MyExtension extension = new MyExtension(getController());
Test.startTest();
extension.someMethod();
Test.stopTest();
// assert on behavior
}