1

I have a class I want to test that has an associated test class with it. I am wondering if I use a TestDataFactory class if the @TestSetup annotation will allow me to access the data inserted from my test class

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

@IsTest
public class TestFunctionalClass {
    //unit tests that test FunctionClass logic
    //query for data that is setup in TestDataFactory.class
}

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

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

I know I can just have the setup method in my TestFunctionalClass but I'd like for all test data to be in my DataFactory class.

6

@TestSetup is only applicable in the same test class.

You should take this approach to make your TestDataFactory class 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
    }
}

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.