ApexSensei

Drills › Prove it with tests

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 returns Urgent
  • verify(null, 'Unscored') → passes and returns Unscored
  • verify(80, 'Standard') → the test fails with an AssertException

Requirements:

  1. Keep the service result in actual.
  2. Compare expected with actual using Assert.areEqual, with a short message.
  3. Return actual after the assert.
  4. Keep verify public 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.

Try this drill free

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

Create isolated test data ›