Create isolated test data
Prove it with tests · Beginner · 12 min · Runs in your Salesforce Developer org · Apex Path
Scenario. A service test must work the same in an empty scratch org and in a busy sandbox.
Insert your own test Account, then read it back from the database by its Id.
What you will learn
A test should create the records it needs. Salesforce rolls back test data when the test ends, so nothing is left in the org.
insert record;
Account fresh = [SELECT Name FROM Account WHERE Id = :record.Id];
Gotcha. Tests run with SeeAllData=false. They can't see the org's Accounts, only rows the test inserts. Setup objects such as User and Profile are still visible.
The task
Finish the two helpers so a test inserts its own Account and reads it back fresh from the database.
Examples:
createOwned('alpha')→ an Account namedAS Test alphawith a real Id- two Accounts created →
loadOwned(second.Id)returns the second one - a field changed after the insert →
loadOwnedreturns the new value
Requirements:
createOwnedinserts the Account it returns, so the Account gets an Id.- Keep the Name
AS Test <token>and the Description<token>. loadOwnedruns a new query every time, filtered by the exact Id, and selects Name and Description.- Keep both helpers public static. The checks call them directly; your own test method is not graded.
Starter code
@IsTest
public class AS_5E1BF2D4A826_RecordTests {
public static Account createOwned(String token) {
Account record = new Account(Name = 'AS Test ' + token, Description = token);
return record;
}
public static Account loadOwned(Id recordId) {
return null;
}
@IsTest
static void createsAndLoadsData() {
Account created = createOwned('visible');
Account loaded = loadOwned(created.Id);
Assert.areEqual(created.Name, loaded.Name);
}
}
When it passes
createOwned inserts a tagged Account. loadOwned queries that exact Id every time, so it sees changes made after the insert.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.