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.

1 Answer 1

7

@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
    }
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .