Is it wrong to put ‘insert’ in test class with SeeAllData = true

apexseealldataunit-test

As the Vlocity documentation says, for Integration Procedure Unit Testing from Apex, we must use Salesforce seeAllData property.

In my case, the ideal would be to insert Order data for the test unit. Because this Integration Procedure only works for certain records, otherwise an exception will be thrown.But I saw that SetupTest is prohibited for SeeAllData=true.

I'm afraid that the other orgs don't have the proper type of Order and the exception will be thrown during the deploy.

So is it wrong to create a @IsTest makeData() to insert test data? What could I do in this case?

@isTest(seeAllData = true)
global class testUnitIP {

    @isTest
    public static List <Order> makeData() {

        Account acc = new Account();
        acc.Name = 'Account Business';
        insert acc;

        Order ord = OrderTestMethods.createOrder(acc.Id, 'International RecordType', 'In progress');
        insert ord;

        return [SELECT id FROM ORDER WHERE Id =: ord.id];
    }

    public static Set <String> setOrderId() {
        return (new Map <String, Order> (makeData())).keySet();
    }
    
    @isTest
    public static void CallIPTest() {
        Boolean hasError = false;
        try {
            VerifyOrder.callIP(setOrderId());
        } catch (Exception e) {
            hasError = true;
        }
        System.assert(hasError, 'Exception was not thrown');
    }
}

Best Answer

If you need a setup method like you describe, you do so without using @isTest (which would make it count as a unit test):

static void makeData() {
  Account a = createAnAccount();
  Contact c = createAContact(a);
  Order o = createAnOrder(a, c);
}

Where the various methods would do some kind of DML. In fact, this is how we used to do it before @testSetup was introduced, and it's still necessary in some rare cases.

Note that all side effects (inserts, updates, deletes, undeletes, whatever) are all rolled back as part of the test, so your production data will never be affected by anything that happens in the unit test. SeeAllData merely grants the unit test the ability to see production data it otherwise would not.