2

I want to create data in a test class that can be used by multiple test methods. However, for the test method to be able to access that test data, I need a way to pass the record IDs to from the @TestSetup method to the @isTest methods. Unfortunately, using static variables doesn't work- those get reset after the @TestSetup method.

Is there a way I can pass data like that?

2

I don't think that is possible today. The only way is through querying records in the test methods.

In the meanwhile you can upvote for the idea Remember static variables set during testSetup annotated methods

1

Salesforce has created @testSetup for data isolation from test class methods and this data once created can be utilized from all the methods.

It could not a good design if data to be created and passed to the @isTest methods which is used from testing.

Think about usage of testsetup method which can be used from multiple classes following this design where TestDataFactory class is reusable and that setup() method can be accessed from @TestSetup method of calling class.

public class FunctionalClass {
    //logic that needs to be tested

}

@IsTest
public class TestFunctionalClass {

    @TestSetup static void prepareData()
    {
        TestDataFactory.setup();
    }

    static testMethod void validateData()
    {
        //unit tests that test FunctionClass logic
        //query for data that is setup in TestDataFactory.class
    }

}

@IsTest
public class TestDataFactory {
    static void setup() {
        //insert test data
    }

    public static List<ExternalObject__x> getExternalObjectTestData() {
        //return mock external objects
    }
}
0

I am not sure if it is the best approacch. But I have used a text field that I can set a specific value for each test scenario. On the test method, I query using this text field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.