Assert service outcomes
Prove it with tests · Beginner · 10 min · Runs in your Salesforce Developer org · Apex Path
Scenario. A Case-routing service turns a score into a priority. Its tests must fail when a score gets the wrong priority.
Write a test helper that fails the test when a service returns the wrong label.
What you will learn
A test needs an expected value to compare with the real result. Assert.areEqual(expected, actual, message) stops the test with an AssertException when the two differ. The happy path is the normal case where nothing goes wrong.
Assert.areEqual('Urgent', actual, 'Score 80 should be Urgent.');
Gotcha. A helper that only returns the result has no assert. It passes even when the service is wrong.
The task
Finish verify so the test fails whenever the service's label is not the expected one.
Examples:
verify(80, 'Urgent')→ passes and returnsUrgentverify(null, 'Unscored')→ passes and returnsUnscoredverify(80, 'Standard')→ the test fails with anAssertException
Requirements:
- Keep the service result in
actual. - Compare
expectedwithactualusingAssert.areEqual, with a short message. - Return
actualafter the assert. - Keep
verifypublic static. The checks call it with right and wrong labels. Your own test method is for practice and is not graded.
Starter code
@IsTest
public class AS_A666623FE257_PriorityTests {
public static String verify(Integer score, String expected) {
String actual = AS_A666623FE257_Priority.label(score);
return null;
}
@IsTest
static void urgentScore() {
verify(80, 'Urgent');
}
}
When it passes
verify returns the label when the expected label is right, and fails the test with an assert when the expected label is wrong.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.