ApexSensei

Drills › Prove it with tests

Prove bulk behavior

Prove it with tests · Intermediate · 15 min · Runs in your Salesforce Developer org · Apex Path

Scenario. A name-cleaning service gets small lists and full 200-record trigger batches.

Write a list verifier that checks the size and every item, and fails on a wrong 200-item list.

What you will learn

Bulk tests use real batch sizes, like 200 records, and check every result, not just the first one. Compare the sizes first, then each position.

Assert.areEqual(expected.size(), actual.size());
for (Integer i = 0; i < expected.size(); i++) {
    Assert.areEqual(expected[i], actual[i]);
}

Gotcha. Checking only size(), or only the first item, misses a wrong value in the middle.

The task

Finish verify so it fails the test when any item, or the number of items, differs from what you expect.

Examples:

  • [' NORTH ', 'South'] with expected ['north', 'south'] → passes
  • 200 names where expected item 150 is wrong → the test fails with an assert
  • 200 names but only 199 expected → the test fails with an assert

Requirements:

  1. Assert that expected and actual have the same size before you read any item.
  2. Assert every item at the same index, including null and repeated inputs.
  3. Return actual when everything matches.
  4. Keep verify public static. The checks call it with right and wrong expected lists.

Starter code

@IsTest
public class AS_5B99BDC873CD_BulkTests {
    public static List<String> verify(List<String> inputs, List<String> expected) {
        List<String> actual = AS_5B99BDC873CD_NameService.normalize(inputs);
        return new List<String>();
    }

    @IsTest
    static void normalizesNames() {
        List<String> expected = new List<String>{ 'north', 'south' };
        Assert.areEqual(expected, verify(new List<String>{ '  NORTH ', 'South' }, expected));
    }
}

When it passes

verify passes for correct expected lists of any size, and fails with an assert when an item or the list size is wrong.

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Reset limits around the act · Use a fake collaborator ›