Sub topic 9
Unit Testing the Action
This chapter assumes you can write basic Apex, know what @IsTest classes are, and have the GetOpenCasesAction class from the earlier chapters. You will test the invocable method directly, the same way the agent calls it: with a list of requests and a list of responses back.
Why test the method directly
An invocable method is a plain static method. You do not need an agent to test it. A unit test builds Request objects, calls the method, and checks the Response objects. Salesforce also requires at least 75% code coverage before you can deploy to production, and meaningful assertions catch bugs that coverage alone does not.
The tests below cover the behaviors the agent depends on:
- A populated result with the right count and summary text.
- An empty result with the message the agent will read back to the user.
- One response per request, in the same order as the requests.
The basic tests
Create test data, build a request, and call the method between Test.startTest() and Test.stopTest(). Use Assert class methods, which are the current standard over the older System.assertEquals.
@IsTest
private class GetOpenCasesActionTest {
@IsTest
static void returnsOpenCaseSummary() {
Account acct = new Account(Name = 'Test Account');
insert acct;
insert new Case(AccountId = acct.Id, Subject = 'Printer is down', Status = 'New');
GetOpenCasesAction.Request req = new GetOpenCasesAction.Request();
req.accountId = acct.Id;
Test.startTest();
List<GetOpenCasesAction.Response> results = GetOpenCasesAction.getOpenCases(
new List<GetOpenCasesAction.Request>{ req }
);
Test.stopTest();
Assert.areEqual(1, results.size());
Assert.areEqual(1, results[0].caseCount);
Assert.isTrue(results[0].summary.contains('Printer is down'));
}
@IsTest
static void returnsMessageWhenNoCases() {
Account acct = new Account(Name = 'Empty Account');
insert acct;
GetOpenCasesAction.Request req = new GetOpenCasesAction.Request();
req.accountId = acct.Id;
List<GetOpenCasesAction.Response> results = GetOpenCasesAction.getOpenCases(
new List<GetOpenCasesAction.Request>{ req }
);
Assert.areEqual(0, results[0].caseCount);
Assert.areEqual('No open cases were found for this account.', results[0].summary);
}
}The second test checks the exact empty-result string. That string is what the agent will explain to the user, so a change to it should fail a test.
What the test flow looks like
Common errors
- Testing only the happy path. The agent will send missing inputs. Add a test where
accountIdis null and confirm you get a response, not an exception. - Relying on org data. Tests do not see existing org data by default. Create everything the test needs. Avoid
@IsTest(SeeAllData=true). - Checking only
results[0]. Because the method is bulkified, the real risk is a wrong mapping between requests and responses. Test with several requests. - Ignoring user mode. The method queries with
WITH USER_MODE. Run a test as a user without Case access and confirm the behavior you want. This is described below.
Testing bulk behavior
Pass two accounts with different case counts, and check that each response matches its own request. This verifies the ordering rule that the platform relies on.
@IsTest
private class GetOpenCasesActionBulkTest {
@IsTest
static void keepsResponsesInRequestOrder() {
Account withCase = new Account(Name = 'Has Case');
Account withoutCase = new Account(Name = 'No Case');
insert new List<Account>{ withCase, withoutCase };
insert new Case(AccountId = withCase.Id, Subject = 'Login fails', Status = 'New');
GetOpenCasesAction.Request first = new GetOpenCasesAction.Request();
first.accountId = withoutCase.Id;
GetOpenCasesAction.Request second = new GetOpenCasesAction.Request();
second.accountId = withCase.Id;
List<GetOpenCasesAction.Response> results = GetOpenCasesAction.getOpenCases(
new List<GetOpenCasesAction.Request>{ first, second }
);
Assert.areEqual(2, results.size());
Assert.areEqual(0, results[0].caseCount);
Assert.areEqual(1, results[1].caseCount);
}
}Testing as a restricted user
To check that the action respects the running user's access, use System.runAs() with a user whose profile lacks Case read access. With WITH USER_MODE, the query throws a QueryException. Decide whether that is the behavior you want, or whether the method should catch it and return a readable message (see the error handling chapter). Then assert on that choice.
Exercise
Why does the bulk test put the account without cases first in the request list, and what bug does it catch?
Show answer
Placing the empty account first means a method that builds responses in query-result order, or skips empty accounts, would return the wrong response at index 0. Asserting that index 0 has a count of 0 and index 1 has a count of 1 proves the method returns exactly one response per request, in request order.