Advanced
25 min read
Creating Test Data
By the end of this lesson, you'll be able to:
- Explain why each test needs its own fresh data, not shared production data
- Insert test sObjects inside a test method
- Recognize the automatic test-data isolation Salesforce provides
Prerequisites: "Your First Test Class"
Tests never see real org data
@isTest
static void testSomething() {
List<Account> accounts = [SELECT Id FROM Account]; // returns EMPTY, even in a full production org
}
@isTest— marks the test method.static void testSomething() {— the test.List<Account> accounts = [SELECT Id FROM Account];— by design, Salesforce automatically isolates every test's data — this query never sees real, existing records, even if the org has millions ofAccountrecords.}— closes the test method.
Creating the data each test needs
@isTest
private class EmployeeDirectoryTest {
@isTest
static void addEmployeeStoresTheRecord() {
EmployeeDirectory directory = new EmployeeDirectory();
Employee emp = new Employee('E001', 'Amara Nkosi', 'amara@example.com');
Boolean added = directory.addEmployee(emp);
System.assertEquals(true, added);
System.assertEquals(1, directory.getEmployeeCount());
}
}
@isTest— marks the class.private class EmployeeDirectoryTest {— the class.@isTest— marks the test method.static void addEmployeeStoresTheRecord() {— the test.EmployeeDirectory directory = new EmployeeDirectory();— sinceEmployeeDirectory(Module 17) is a plain Apex class, not ansObject, this test just creates it directly withnew.Employee emp = new Employee('E001', 'Amara Nkosi', 'amara@example.com');— same forEmployee— no DML or SOQL involved at all.- (blank line)
Boolean added = directory.addEmployee(emp);— runs the code being tested.- (blank line)
System.assertEquals(true, added);— asserts the add succeeded.System.assertEquals(1, directory.getEmployeeCount());— asserts the count reflects it.}— closes the test method.}— closes the class.
Inserting sObject test data
@isTest
private class LibraryServiceTest {
@isTest
static void addBookInsertsARecord() {
LibraryService library = new LibraryService();
Book__c book = library.addBook('The Long Walk', 'Richard Bachman', '978-0-451-14098-6');
System.assertNotEquals(null, book.Id);
List<Book__c> allBooks = [SELECT Id, Title__c FROM Book__c];
System.assertEquals(1, allBooks.size());
}
}
@isTest— marks the class.private class LibraryServiceTest {— the class.@isTest— marks the test method.static void addBookInsertsARecord() {— the test.LibraryService library = new LibraryService();— creates the service.- (blank line)
Book__c book = library.addBook('The Long Walk', 'Richard Bachman', '978-0-451-14098-6');— performs a realinsert(Module 22).- (blank line)
System.assertNotEquals(null, book.Id);— confirms it was actually saved.- (blank line)
List<Book__c> allBooks = [SELECT Id, Title__c FROM Book__c];— because tests run in their own isolated data, this confidently finds exactly the one book this test itself created.System.assertEquals(1, allBooks.size());— never any leftover data from another test or from production.}— closes the test method.}— closes the class.
Exercise
Write a test method that inserts an Account with a Name, then queries it back and asserts the Name matches.
Show hint
insert acc; then re-query by Id and assertEquals on the Name.
APEX
Creating Test Data Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Every test runs against a completely empty, isolated copy of the org's data — meaning every test method must create whatever records it needs itself, and nothing it inserts ever touches real data.