This guide walks through 18 writing @isTest classes with proper test data, assertions, and bulk/negative-path coverage. Each entry below includes the full problem statement, the governor-limit and best-practice constraints it's testing, and a numbered approach to solving it — then you can open the same problem in ApexArena's browser-based Apex editor and get instant pass/fail feedback against real test cases.
Create a TestDataFactory class with static methods to generate
realistic test data for Account, Contact, and
Opportunity records. Support both insert and no-insert modes.
createContact must accept an Account parametercreateOpportunity must have valid StageName and CloseDateWrite a complete @isTest class for the AccountRatingTrigger
(Problem 1). The test class must follow all Apex testing best practices.
@TestSetup to create shared test dataRating = 'Hot' for revenue > 1MRating = 'Cold' for revenue < 100KSystem.assertEquals() for assertions (not just assert)Write a test class AccountRatingTriggerTest with at least 3 test methods
covering the Account Rating trigger:
@isTest annotation on the class and each methodTest.startTest() / Test.stopTest()System.assertEquals() for assertionsWrite a test class ContactDuplicateTest that tests the Contact duplicate-prevention trigger:
DmlExceptiontry/catch with DmlException in the negative testWrite a test class OpportunityHelperTest that achieves 100% coverage of
OpportunityHelper.isWon() and isLost().
Include test methods for:
Write a test class BulkTriggerTest that validates that a trigger on
Account handles 200 records in a single DML without hitting
governor limits.
Requirements:
insert callLimits.getQueries() ≤ 5 (low SOQL usage)Limits.getDmlStatements() ≤ 3Write a test class AccountServiceTest for AccountService:
@testSetupgetHotAccounts() returns only accounts with Rating = 'Hot'deactivateSmallAccounts() updates the right recordsWrite a test class HttpCalloutServiceTest that mocks an HTTP callout
using HttpCalloutMock.
Steps:
HttpCalloutMockTest.setMock()HttpCalloutService.callWithRetry() and assert the responseWrite a test class DeactivateOldContactsBatchTest:
LastActivityDate 400 days agoDatabase.executeBatch()Active__c = false after batch runsEvery problem above runs in a real in-browser Apex editor with instant pass/fail feedback and best-practice linting. No Salesforce org needed.
Create Free Account →A trigger + handler already exist that set Account.Rating based on
AnnualRevenue (Hot > 1M, Warm 100K–1M, Cold < 100K).
Your task is to write a complete test class.
@isTest on every test method.Test.startTest() / Test.stopTest() wrapping DML.System.assertEquals for all assertions.A trigger + handler prevent two Contacts sharing the same Email.
Write a comprehensive test class covering all scenarios below.
DmlException with "already exists".UPPER@test.com blocks upper@test.com.A handler exists that on before update of a Case:
'Escalated' (new): sets Priority = 'High'
and Escalation_Reason__c = 'SLA Breach — auto-escalated'.'Escalated': calls
addError('Case is already escalated.').Write a full test class covering all six scenarios:
Database.update(false).Write a complete @isTest class for NightlyStaleLeadLauncher
(from the previous problem — a Schedulable that launches FlagStaleLeadsBatch)
that verifies both:
CronTrigger with the right cron expression.execute() method genuinely launches the batch and the batch's logic runs correctly.testSchedulingRegistersACronTrigger: call System.schedule(...)
inside Test.startTest()/Test.stopTest(), then query
CronTrigger by the returned job Id and assert its
CronExpression and NextFireTime.testExecuteLaunchesTheBatch: insert a stale Lead, call
new NightlyStaleLeadLauncher().execute(null) directly inside
Test.startTest()/Test.stopTest(), then re-query the Lead and
assert Status = 'Stale'.Test.startTest()/Test.stopTest()
in Salesforce's test framework — but asserting on the CronTrigger record
(rather than assuming side effects happened) is the more precise, deterministic way to
verify the scheduling itself worked.execute(null) directly is the standard way to test a Schedulable's
actual business logic — you don't need a real cron fire to exercise what
execute() does.Write a test class OpportunityForecastTriggerTest with test methods
covering the Opportunity forecast-category trigger across multiple distinct
scenarios in one bulk operation: a high-probability deal, a mid-probability
deal, and a zero-amount deal, all inserted together.
Test.startTest()/Test.stopTest().Forecast_Category_Custom__c independently.Write a test class PartialInsertHandlerTest that verifies code using
Database.insert(records, false) (allow-partial-success mode) correctly
separates successful records from failed ones by inspecting the returned
List<Database.SaveResult>.
SaveResult list, checking isSuccess() per
record and asserting the success/failure count matches expectations.Write a test class AccountLifecycleTest that covers a two-step
lifecycle: insert an Account in one transaction, then update it in a
second, separately-timed transaction, asserting the state after each
step.
Write a test class DiscountConfigLookupTest that verifies a service
method gracefully handles the case where its expected
Discount_Config__mdt record is missing — it should return a safe default
rather than throwing.
0) instead of
raising an unhandled exception.Write a test class IsolatedStepRunnerQueueableTest that verifies
IsolatedStepRunnerQueueable continues processing the rest of its Ids even
when one record in the middle of the list fails to update (e.g. an invalid Id).
Test.startTest()/Test.stopTest()
so it actually executes synchronously by the time the test continues.Write and run real Apex code right in your browser — instant pass/fail feedback, best-practice linting, and governor limit monitoring. No Salesforce org needed.
Create Free Account → Explore All ProblemsApexArena is a free, browser-based Salesforce Apex coding practice platform covering every major topic tested on the Salesforce Platform Developer I (PD1) and Platform Developer II (PD2) certification exams. All problems run directly in your browser with instant pass/fail feedback, best-practice linting (SOQL in loops, DML in loops, empty catch blocks), and governor limit monitoring — no Salesforce Developer Edition org required.
Related tutorials: Apex Triggers · SOQL · Batch Apex · Interview Q&A · Governor Limits