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
}
  1. @isTest — marks the test method.
  2. static void testSomething() { — the test.
  3. 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 of Account records.
  4. } — 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());
    }
}
  1. @isTest — marks the class.
  2. private class EmployeeDirectoryTest { — the class.
  3. @isTest — marks the test method.
  4. static void addEmployeeStoresTheRecord() { — the test.
  5. EmployeeDirectory directory = new EmployeeDirectory(); — since EmployeeDirectory (Module 17) is a plain Apex class, not an sObject, this test just creates it directly with new.
  6. Employee emp = new Employee('E001', 'Amara Nkosi', 'amara@example.com'); — same for Employee — no DML or SOQL involved at all.
  7. (blank line)
  8. Boolean added = directory.addEmployee(emp); — runs the code being tested.
  9. (blank line)
  10. System.assertEquals(true, added); — asserts the add succeeded.
  11. System.assertEquals(1, directory.getEmployeeCount()); — asserts the count reflects it.
  12. } — closes the test method.
  13. } — 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());
    }
}
  1. @isTest — marks the class.
  2. private class LibraryServiceTest { — the class.
  3. @isTest — marks the test method.
  4. static void addBookInsertsARecord() { — the test.
  5. LibraryService library = new LibraryService(); — creates the service.
  6. (blank line)
  7. Book__c book = library.addBook('The Long Walk', 'Richard Bachman', '978-0-451-14098-6'); — performs a real insert (Module 22).
  8. (blank line)
  9. System.assertNotEquals(null, book.Id); — confirms it was actually saved.
  10. (blank line)
  11. 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.
  12. System.assertEquals(1, allBooks.size()); — never any leftover data from another test or from production.
  13. } — closes the test method.
  14. } — 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

1. What data does a SOQL query inside a test method see?

Log in to submit the quiz and save your score.

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.