This guide walks through 12 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 runsA 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 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