How to use the annotated @TestSetup method in Tests?
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
}
It is a correct way. I would, however, like to identify few benefits of @testSetup
below
- Use this annotation if you want to create test data once and use it in all test methods of your class. Therefore, you don't need to recreate the data again.
- Records created in a test setup method are rolled back at the end of test class execution.
- @testSetup methods are executed first in the class before any test methods.
You may have multiple @testSetup methods in your class, however, their execution order is not guaranteed.
See also:
Test Setup Method Considerations
Test setup methods are supported only with the default data isolation mode for a test class. If the test class or a test method has access to organization data by using the @isTest(SeeAllData=true) annotation, test setup methods aren’t supported in this class.
Multiple test setup methods are allowed in a test class, but the order in which they’re executed by the testing framework isn’t guaranteed.- You can have only one test setup method per test class.
- If a fatal error occurs during the execution of a test setup method, such as an exception that’s caused by a DML operation or an assertion failure, the entire test class fails, and no further test methods are executed.
- If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.