Skip to content

How to Test Step

Testing your business logic in Apex ensures that Step implementations execute predictably across different transaction contexts. Breezz provides a dedicated testing utility—forvendi.BreezzApi.TESTS—to streamline test initialization, state isolation, error assertion, and record loading.


Key Testing Approaches

When writing unit tests for Breezz Steps, you typically choose between two main strategies:

  1. End-to-End Trigger Test (Database Context): Executes actual DML statements (insert, update) against real database records. Requires active Trigger and Step Group configurations in the test org state.
  2. Direct Step Execution (ModificationContext Inspection): Uses forvendi.BreezzApi.STEPS.build() to execute Step logic in-memory without firing database triggers. Inspects the resulting ModificationContext directly.

Example 1: Testing CountEmployeesStep

This example demonstrates both testing strategies for a Step that updates Account.NumberOfEmployees based on associated Contact records.

@IsTest
private class CountEmployeesStepTest {

    @TestSetup
    static void testSetup() {
        // Initialize Breezz framework test environment
        forvendi.BreezzApi.TESTS.init('BreezzPlugin');
    }

    @IsTest
    static void when_CountEveryContactAsEmployee_expect_ContactAddedToAccount() {
        // Strategy 1: Full Trigger + Database Integration Test
        Account acc = new Account(Name = 'Employer');
        insert acc;

        Contact[] cnts = new Contact[] {
            new Contact(LastName = 'Johnson', AccountId = acc.Id),
            new Contact(LastName = 'Jordan', AccountId = acc.Id)
        };

        Test.startTest();
        insert cnts;
        Test.stopTest();

        // Load record state using Breezz testing helper
        Account account = (Account) forvendi.BreezzApi.TESTS.loadRecordById(acc.Id);

        // Verify framework logs and field assertions
        forvendi.BreezzApi.TESTS.assertErrorLogs();
        Assert.isNotNull(account);
        Assert.areEqual(2, account.NumberOfEmployees);
    }

    @IsTest
    static void when_ExecutedCountEmployeesStep_expect_CountEveryContactAsEmployee() {
        // Strategy 2: Direct Step Execution with ModificationContext Inspection
        Account acc = new Account(Name = 'Employer');
        insert acc;

        Contact[] cnts = new Contact[] {
            new Contact(LastName = 'Johnson', AccountId = acc.Id),
            new Contact(LastName = 'Jordan', AccountId = acc.Id)
        };
        insert cnts;

        Test.startTest();
        // Use skipModificationContextSave() to preserve queued DMLs in-memory for assertion
        forvendi.ModificationContext ctx = forvendi.BreezzApi.STEPS.build()
            .skipModificationContextSave()
            .addStep(new CountEmployeesStep())
            .execute(cnts);
        Test.stopTest();

        forvendi.BreezzApi.TESTS.assertErrorLogs();
        
        // Assert field changes staged inside ModificationContext
        Assert.isNotNull(ctx.getRecordToUpdate(acc.Id));
        Assert.areEqual(2, ctx.getRecordToUpdate(acc.Id).get('NumberOfEmployees'));
    }
}

💡 Why use skipModificationContextSave()?
By default, STEPS.build().execute() clears the staged DML modifications after committing changes. Calling .skipModificationContextSave() keeps the ModificationContext populated so you can inspect queued operations (e.g., getRecordToUpdate()) during test execution.


Example 2: Testing Complex Logic (LeadConverter)

When testing Steps that produce new related records (Account, Contact, Opportunity) without returning explicit IDs, use forvendi.BreezzApi.TESTS.loadAllRecords() to retrieve created SObject collections:

@IsTest
private class LeadConverterTest {

    @TestSetup
    static void testSetup() {
        forvendi.BreezzApi.TESTS.init('BreezzPlugin');
    }

    @IsTest
    static void when_ProvidedLeadRecord_expect_ConvertLead() {
        Lead[] leads = new Lead[] {
            new Lead(LastName = 'LastName', MobilePhone = '1234', Company = 'Company')
        };
        insert leads;

        Test.startTest();
        leads[0].Status = 'Closed - Converted';
        update leads;

        // Execute Step directly against modified lead collection
        forvendi.BreezzApi.STEPS.build()
            .addStep(new LeadConverter())
            .execute(leads);
        Test.stopTest();

        // Retrieve all generated target records across SObject types
        Opportunity[] opps = forvendi.BreezzApi.TESTS.loadAllRecords(Opportunity.getSObjectType());
        Account[] accs = forvendi.BreezzApi.TESTS.loadAllRecords(Account.getSObjectType());

        forvendi.BreezzApi.TESTS.assertErrorLogs();
        Assert.areEqual(1, opps.size());
        Assert.areEqual(1, accs.size());
    }
}

Example 3: Testing Asynchronous Steps

When testing asynchronous Steps, queued background worker tasks do not automatically execute synchronously within standard Apex unit test contexts. The Breezz Framework provides specialized test helpers—such as deliverAsyncQueueEvents() and deliverDelayedAsyncJobs()—to flush enqueued async events synchronously between Test.startTest() and Test.stopTest().

@IsTest
private class ProcessingCreateContactTest {

    @TestSetup
    static void testSetup() {
        // Initialize Breezz framework test mocks and active plugin
        forvendi.BreezzApi.TESTS.init('BreezzPlugin');
    }

    @IsTest
    static void when_ExecuteContactGenerator_expect_GenerateContactForEveryAccount() {
        Account[] accs = new Account[]{
            new Account(Name = 'New Account 1'),
            new Account(Name = 'New Account 2')
        };
        insert accs;

        Test.startTest();
        // Enqueue async jobs via Step builder
        forvendi.ModificationContext ctx = forvendi.BreezzApi.STEPS.build()
            .addStep(new ProcessingCreateContact())
            .execute(accs);

        // Force immediate synchronous execution of enqueued asynchronous tasks
        forvendi.BreezzApi.TESTS.deliverAsyncQueueEvents();
        Test.stopTest();

        // Verify framework logs and database results
        forvendi.BreezzApi.TESTS.assertErrorLogs();

        List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accs];
        Assert.areEqual(2, contacts.size());
    }
}

Essential Breezz Testing Helpers

Method Description
TESTS.init('PluginName') Initializes test mocks and sets up the active Breezz Apex Plugin implementation.
TESTS.loadRecordById(recordId) Queries and returns a fresh instance of the specified record by ID.
TESTS.loadAllRecords(SObjectType) Queries and returns all records of a given SObject type created within the test transaction context.
TESTS.deliverAsyncQueueEvents() Immediately processes enqueued AsyncJob items in memory during test execution.
TESTS.deliverDelayedAsyncJobs() Flushes and processes pending DelayedAsyncJob items queued via addDelayedAsyncJob().
TESTS.assertErrorLogs() Asserts that no framework execution errors or unhandled exception logs were recorded.

💡 Apex API Reference: To learn more about testing utilities and test harness methods, refer to Breezz APEX API - TESTS Utility.