ApexSensei

Drills Security & deployment

Choose a deploy test level

Security & deployment · Beginner · 10 min · Runs in your Salesforce Developer org · Apex Path

Return the Metadata API test level for a change. Apex uses specified tests, profile and layout use local tests, and anything else, including null, uses every test in the org.

Lesson

Why this matters

A production deploy without tests can ship a broken class. The Metadata API testLevel chooses how many tests run. Apex-only changes can name their tests. A profile or layout change should run the org's local tests. When the risk is unknown, run everything, including managed packages.

The smallest working example

if (change == 'apex') {
    return 'RunSpecifiedTests';
}
if (change == 'profile' || change == 'layout') {
    return 'RunLocalTests';
}
return 'RunAllTestsInOrg';

String == ignores case, so 'Apex' matches 'apex'.

Name the parts

  • **RunSpecifiedTests** — only the tests you name. Use it for Apex.
  • **RunLocalTests** — every test in the org except managed packages. Use it for profile and layout.
  • **RunAllTestsInOrg** — every test, including managed packages. The default, including null and blank.
  • **NoTestRun** is not a passing answer here.

A common wrong version

if (change.equals('apex')) {
    return 'RunSpecifiedTests';
}

equals throws when change is null. == does not.

The fix

Use == for the named cases. Return RunAllTestsInOrg from the last line so null and blank are covered.

Now you try

Finish testLevelFor. Keep the method name and the String parameter.

Learn more

The challenge

Write testLevelFor(String change) that returns the Metadata API test level name for that change.

Examples:

  • testLevelFor('apex') → RunSpecifiedTests
  • testLevelFor('profile') → RunLocalTests
  • testLevelFor('unknown') or null → RunAllTestsInOrg

Requirements:

  1. testLevelFor('apex') should be RunSpecifiedTests.
  2. testLevelFor('profile') should be RunLocalTests.
  3. testLevelFor('layout') should be RunLocalTests.
  4. testLevelFor(null) should be RunAllTestsInOrg.
  5. testLevelFor('Apex') should be RunSpecifiedTests.

Starter code

String testLevelFor(String change) {
    return null;
}

Related drills

Try this drill free

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

‹ Gate work behind a feature flag